Fgets( ) function can limit the number of characters read and thus prevent array overruns. - C File

C examples for File:Text File

Introduction

fgets( ) does not remove the newline character and gets_s( ) does,

So you will have to manually remove it.

Demo Code

#include <stdio.h>
#include <string.h>

int main(void)
{
   char str[80];/*from w  ww.  jav  a  2 s .c  o  m*/
   int i;

   printf("Enter a string: ");
   fgets(str, 10, stdin);

   /* remove newline, if present */
   i = strlen(str) - 1;
   if (str[i] == '\n') str[i] = '\0';

   printf("This is your string: %s", str);

   return 0;
}

Result


Related Tutorials