I/O behaves unexpectedly

Top  Previous  Next

switching file directly from input to output


If you change directly from input to output on the same file, or vice versa, you are likely read or write wrong data. You must insert a call to fseek between the two.

I/O function returns negative value


printf and other printing functions return a negative value as an indication that an error has occurred.

I/O function returns non-zero value


Certain functions, including fclose, ferror, fgetpos, remove and rename, indicate an error by returning a non-zero value. See the documentation of the various functions for details.

I/O function returns zero


The fread and fwrite functions return zero as an error indicator (fread uses zero to indicate end-of-file as well).

I/O function returns EOF


A number of functions, including fgetc, fflush, fputc, fscanf, getc, getchar, putchar, putc, and scanf, return EOF as an error indicator. The input functions among them also return EOF to indicate end-of-file. See also ungetch.

I/O function returns NULL


The functions gets, fgets, fopen, and freopen return NULL to indicate an error. The same value is returned by gets and fgets to indicate an end-of-file.

ungetc cannot un-get a character


The ability to return characters to the input stream is limited. See the documentation of ungetc.