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];/*w w  w  . j  a  va2s  .com*/
   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