Is there any way to peek at the stdin buffer?

Portably, you can get the next character in the input stream with getchar() and then push it back with ungetc(), which results in a state as if the character wasn’t removed from the stream.

The ungetc function pushes the character specified by c (converted to an unsigned char) back onto the input stream pointed to by stream. Pushed-back characters will be returned by subsequent reads on that stream in the reverse order of their pushing.

Only one character of pushback is guaranteed by the standard, but usually, you can push back more.

As mentioned in the other answers resp. the comments there, in practice, you can almost certainly peek at the buffer if you provide your own buffer with setvbuf, although that is not without problems:

If buf is not a null pointer, the array it points to may be used instead of a buffer allocated by the setvbuf function

that leaves the possibility that the provided buffer may not be used at all.

The contents of the array at any time are indeterminate.

that means you have no guarantee that the contents of the buffer reflects the actual input (and it makes using the buffer undefined behaviour if it has automatic storage duration, if we’re picky).

However, in practice the principal problem would be finding out where in the buffer the not-yet-consumed part of the buffered input begins and where it ends.

Leave a Comment