Example (fgetc -- Read a Character)

This example gathers a line of input from a stream.

#include <stdio.h>
#define FILENAME "myfile.dat"
#define  MAX_LEN  80
int main(void)
{
   FILE *stream;
   char buffer[MAX_LEN + 1];
   int i, ch;
   stream = fopen("FILENAME","r");
   for (i = 0; (i < (sizeof(buffer)-1) &&
         ((ch = fgetc(stream)) != EOF) && (ch != '\n')); i++)
      buffer[i] = ch;
   buffer[i] = '\0';
   if (fclose(stream))
      perror("fclose error");
   printf('The input line was: %s\n", buffer);
   return 0;
   /******************************************************
      If FILENAME contains:  one two three
      The output should be:
      The input line was: one two three
   ******************************************************/
}