I know in C++, you're able to peek at the next character by using: in.peek();
.
How would I go about this when trying to "peek" at the next character of a file in C?
the 'peek' function on input streams (in your case cin ) retrieves the next character from the stream without actually consuming it. That means that you can "preview" the next character in the input, and on the next call to any consuming operation (overloaded operator >> or cin.
The ungetc() function pushes the unsigned character c back onto the given input stream. However, only one consecutive character is guaranteed to be pushed back onto the input stream if you call ungetc() consecutively. The stream must be open for reading. A subsequent read operation on the stream starts with c.
fgetc+ungetc. Maybe something like this:
int fpeek(FILE *stream) { int c; c = fgetc(stream); ungetc(c, stream); return c; }
You could use a getc
followed by an ungetc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With