Read the next character from a stream.
#include <stdio.h> int fgetc(FILE *stream);
stream
A pointer to a file stream.
The fgetc() function reads the next character from stream and advances its file position indicator.
fgetc() returns the character as an unsigned char converted to an int . If the end-offile has been reached or a read error is detected, fgetc() returns EOF. The difference between a read error and end-of-file can be determined by the use of feof().
If the file is opened in update mode (+) a file cannot be read from and then written to without repositioning the file using one of the file positioning functions ( fseek(), fsetpos(), or rewind()) unless the last read or write reached the end-of-file.
This facility may have limited capability on configurations of the EWL that run on platforms that do not have console input/output or a file system.
#include <stdio.h> #include <stdlib.h> int main(void) { FILE *f; char filename[80], c; // get a filename from the user printf("Enter a filename to read.\n"); gets(filename); // open the file for input if (( f = fopen(filename, "r")) == NULL) { printf("Can't open %s.\n", filename); exit(1); } // read the file one character at a time until // end-of-file is reached while ( (c = fgetc(f)) != EOF) putchar(c); // print the character // close the file fclose(f); return 0; } Output: Enter a filename to read. foofoo 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99