Use fgets() function to read from console/stdin - C File

C examples for File:Standard Device

Description

Use fgets() function to read from console/stdin

Demo Code

#include <stdio.h>

#define MAXLEN 10/*  ww  w .  ja v a2  s.com*/

int main( void )
{
   char buffer[MAXLEN];

   puts("Enter text a line at a time; enter a blank to exit.");

   while (1)
   {
        fgets(buffer, MAXLEN, stdin);

        if (buffer[0] == '\n')
            break;

        puts(buffer);
   }
   return 0;
}

Result


Related Tutorials