Will this tell me if the next char in a file exists?

I need to find out if the next char in a ifstream object is the eof without extracting it. Will this work:

1
2
3
4
5
ifstream ptext ("dog.txt");

 if (ptext.peek() && ptext.eof())
    //do blah
rozick1,

actually I think you should use !ptext.eof()

try it like this:

1
2
3
4
5
6
7
    
    char x;
while (ptext.peek() && !ptext.eof())
    {
        ptext.get(x);
        cout << x;
    }

Ah, sorry I may have been unclear. I want the statement following the if to execute IF the next character is eof.
Not a problem, ptext.eof() will only test if the file stream is EOF. you should store what ptext.peek() returns in a variable so you can use it for whatever purpose you want.
i'd imagine it looking somethign like this:

1
2
3
4
5
6
7
8
9
ifstream ptext ("test.txt");
    char x;
while (true)
    {
        x=ptext.peek();
        if (ptext.eof()) {cout << "end of file"; break;}
        cout << x;
        ptext.get(x);
    }


Topic archived. No new replies allowed.