int getc ( 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).
getc and fgetc are equivalent, except that getc may be implemented as a macro in some libraries. See getchar for a similar function that reads directly from stdin.
- Pointer to a FILE object that identifies an input stream.
Because some libraries may implement this function as a macro, and this may evaluate the stream expression more than once, this should be an expression without side effects.
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.
/* getc example: money counter */
int main ()
FILE * pFile;
int n = 0;
if (pFile==NULL) perror ("Error opening file");
c = getc (pFile);
if (c == '$') n++;
} while (c != EOF);
printf ("File contains %d$.\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 ($) does the file contain.
- Get character from stream (function
- Write character to stream (function
- Read block of data from stream (function
- Write block of data to stream (function