Syntax
#include <stdio.h> int fgetc(FILE *stream);Description
fgetc reads a single byte from the input stream at the current position and increases the associated file pointer, if any, so that it points to the next byte.
Note: fgetc is identical to getc but is always implemented as a function call; it is never replaced by a macro.
fgetc returns the byte read as an integer. An EOF return value indicates an error or an end-of-file condition. Use feof or ferror to determine whether the EOF value indicates an error or the end of the file.
This example gathers a line of input from a stream.
#include <stdio.h> #define MAX_LEN 80 int main(void) { FILE *stream; char buffer[MAX_LEN+1]; int i,ch; stream = fopen("myfile.dat", "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 myfile.dat contains: one two three The output should be: The input line was : one two three ****************************************************************************/ }Related Information