You can simply use getc()
to get the next character, followed by a call to ungetc()
.
Update: see @Jonathan's comment for a wrapper that allows you to peek beyond the end of the file (returning EOF
in this case).
Update 2: slightly more compact version:
int fpeek(FILE * const fp) { const int c = getc(fp); return c == EOF ? EOF : ungetc(c, fp); }
source share