Stream States
The valid states, and state transitions, for a stream are shown in the following figure.
Each of the circles denotes a stable state. Each of the lines denotes a transition that can occur as the result of a function call that operates on the stream. Five groups of functions can cause state transitions.
Functions in the first three groups are declared in <stdio.h>:
The byte read functions — fgetc, fgets, fread, fscanf, getc, getchar, gets, scanf, and ungetc
The byte write functions — fprintf, fputc, fputs, fwrite, printf, putc, putchar, puts, vfprintf, and vprintf
Functions in the remaining two groups are declared in <wchar.h>:
The wide read functions — fgetwc, fgetws, fwscanf, getwc, getwchar, ungetwc, and wscanf,
The wide write functions — fwprintf, fputwc, fputws, putwc, putwchar, vfwprintf, vwprintf, and wprintf,
The state diagram shows that you must call one of the position functions between most write and read operations:
You cannot call a read function if the last operation on the stream was a write.
You cannot call a write function if the last operation on the stream was a read, unless that read operation set the end-of-file indicator.
Finally, the state diagram shows that a position operation never decreases the number of valid function calls that can follow.