Hello. I am creating a subclass of ifstream. It's sole purpose is to create a new function peek(n), which allows me to peek n characters ahead instead of just 1 character which the peek() function already does. I just need it so that I can roll my own LALR(2).
Anyways, it's not exactly an extension of peek(). I want peek(n) to return a character, but I don't want it to effect the ifstream state bits.
So here's the problem. I implement peek(n) using seekg and tellg. However, it appears once seekg() returns back EOF, I can't reset the read pointer to what it was so that subsequent get() call returns what you would expect.
So, I need help. Here's what I have for a file ('hello.txt'):
It seems to me that you should only return ifstream::peek() if n = 0, not if n = 1 or above. 0 is the next char and 1 is the one after that etc...
1 2
if(n == 0)
return ifstream::peek();
Also I don't see why you need to check the stream after the read, it makes more sense to peek() the stream and return it to its original state regardless:
1 2 3 4 5 6 7 8 9 10 11
// preserve initial state
oldstate = rdstate();
oldpos = tellg();
// peek relevant char
seekg(n, ios_base::cur);
cread = ifstream::peek();
// restore original state
seekg(oldpos);
clear(oldstate);
Last, your read loop needs to check the stream *after* it reads in the char:
1 2 3 4 5 6 7 8 9 10 11 12
pifstream input("input.txt");
char creader;
char cpeek2;
while(input.get(creader)) // only enter the body if this succeeds
{
cpeek2 = input.peek(2);
cout << "Read char: '" << (char) creader << "'\t" << "Peek(2) : '"
<< (char) cpeek2 << "'" << endl;
}
I thought my correction worked, but it doesn't now. I'm not sure why. Anyways, I tried your's and that works! So, I'm not quite sure if I'm out of the woods.
Regarding if n==0, just call peek(), yeah, I saw that too, but I didn't want to get into it when I posted my first example. But great pair of eyes you got there! :)
I guess I check the results of the read because I wanted to either
seekg(-n, ios_bas::cur) or
seekg( oldpos)
I was / am worred about the performance of each. Anyways, I probably shouldn't worry about performance. I should just get it to work.