I'm need to know if the next char in ifstream
is the end of file. I'm trying to do this with .peek()
:
if (file.peek() == -1)
and
if (file.peek() == file.eof())
But neither works. There's a way to do this?
Edit: What I'm trying to do is to add a letter to the end of each word in a file. In order to do so I ask if the next char is a punctuation mark, but in this way the last word is left without an extra letter. I'm working just with char
, not string
.
You didn't show any code you are working with, so there is some guessing on my part. You don't usually need low level facilities (like
peek()
) when working with streams. What you probably interested in isistream_iterator
. Here is an example,You can also use istreambuf_iterator to work on buffer directly: