Difference with files

Simple question, lets say im opening a file and reading in data


whats the difference between

while(file)

and

while (!file.eof())
while (file)
is effectively the same as
while (!file.fail())

But in practical terms of which is recommended or most useful, while (file) is usually much better. Best to consider eof as a special use for very particular circumstances. Mostly it causes more problems than it solves.

See
http://www.cplusplus.com/reference/ios/ios/operator_bool/
http://www.cplusplus.com/reference/ios/ios/fail/
http://www.cplusplus.com/reference/ios/ios/eof/
AH I see, does the same apply for lets say, traversing in a linked list or acccesing elements in a linked list?

current = new node ...

while (current)

or

while (current->next != NULL)


are both of these the same?
That's an interesting question. I hadn't thought of making that sort of connection. I think I'd have to say no. Some of the ideas and ways of thinking of them might be related, but not the syntax. Files, which is what we started off talking about fall under the category of streams, there can be all sorts of streams, such as keyboard input, file input, or some other, can often be handled with very similar code.

One thing I would say, which is not quite what you were asking. If you use the container classes of the standard library, they can often be treated with similar code. For example you might write some code which works with a vector and then later replace the vector with a list and a lot of the code might remain the same.
Topic archived. No new replies allowed.