O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Single Character I/O

To read a single character from a file that has been opened with a call to fopen and from which reading may occur, use the fgetc function, whose definition is as follows:

int fgetc ( FILE * stream )

This function will read the next character from the input stream, should one exist. If there is no character available, it will return an error value. Because 0 is an appropriate value for a character (if maybe a little unusual), the return value is likely to be -1. However, this might change, depending on the compiler, so you’re advised to check beforehand.

The companion function to fgetc is fputc, which has the following definition:

int fputc ( char character, FILE * stream )

Not surprisingly, this function will write ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required