Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C equivalent to fstream's peek

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?

like image 282
Anthony Avatar asked Jan 17 '10 21:01

Anthony


People also ask

What does peek () mean 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.

What is Ungetc in C?

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.


2 Answers

fgetc+ungetc. Maybe something like this:

int fpeek(FILE *stream) {     int c;      c = fgetc(stream);     ungetc(c, stream);      return c; } 
like image 172
ephemient Avatar answered Sep 22 '22 12:09

ephemient


You could use a getc followed by an ungetc

like image 44
moonshadow Avatar answered Sep 21 '22 12:09

moonshadow