int fgetc ( FILE * stream );
Get character from stream
Returns the character currently pointed by the internal file position indicator of the specified stream. The internal file position indicator is then advanced to the next character.
If the stream is at the end-of-file when called, the function returns EOF and sets the end-of-file indicator for the stream (feof).
If a read error occurs, the function returns EOF and sets the error indicator for the stream (ferror).
fgetc and getc are equivalent, except that getc may be implemented as a macro in some libraries.
- Pointer to a FILE object that identifies an input stream.
On success, the character read is returned (promoted to an int value).
The return type is int to accommodate for the special value EOF, which indicates failure:
If the position indicator was at the end-of-file, the function returns EOF and sets the eof indicator (feof) of stream.
If some other reading error happens, the function also returns EOF, but sets its error indicator (ferror) instead.
/* fgetc example: money counter */
int main ()
FILE * pFile;
int n = 0;
if (pFile==NULL) perror ("Error opening file");
c = fgetc (pFile);
if (c == '$') n++;
} while (c != EOF);
printf ("The file contains %d dollar sign characters ($).\n",n);
This program reads an existing file called myfile.txt character by character and uses the n variable to count how many dollar characters ($) the file contains.
- Get character from stream (function)
- Write character to stream (function)
- Read block of data from stream (function)
- Read formatted data from stream (function)