while(loop)

Hi everyone

I have a struct and made a function iostream& operator >> (something)

now when I use a while loop, it does not read the last part(line) of information

1
2
3
4
5
6
7
8
9
10
11
12
ifstream in;
Sstruct obj;

// open file
// check if it is open

while(in >> obj)
{
// do something in my case 

myVec.push_back(obj);
}


if I do while(!in.eof()){} it works fine.

Why does it read everything but the last line or part of text?

Is it better to use while(eof()) or smth else?

Thanks for the advise
In Visual Studio 2010, the expression uses std::ios_base::operator void*() to determine if the condition is true or not. VS2010's implementation returns a null pointer (which is interpreted as false) if the fail() method returns true.

If this is your case too, then it means that during the last read the file stream invalidates somehow. Find out why fail() returns true and you will have found your problem.
thanks for the reply webJose,

But I have no Idea how to do that, or I am not understanding your explenation vere well

I do consider myself very fresh to C++ and Visual Studio
What I'm saying is that your process of reading the stream into your personalized object is invalidating the stream somehow. You must always check for this either using fail() or good(). Go ahead and debug your program. Set a breakpoint in your operator>>() function and then go step by step. But first change the code to check for the value of good() every time after you extract data from the stream.
I see nothing wrong with your code. Maybe the error is in operator>> for Sstruct?
Last edited on
thanks everyone for the time.

My mistake was on operator >>
had an extra ignore()
Topic archived. No new replies allowed.