What does fstream::getline() return after setting fail bit?

D

Disc Magnet

I wrote this code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
fstream f("foo.txt");
char s[10];

while (f.getline(s, 10)) {
cout << "[" << s << "]" << endl;
cout << "eof: " << f.eof() << "; fail: " << f.fail() << "; "
<< "bad: " << f.bad() << "; good: " << f.good() << endl;
cout << endl;
}
cout << "Outside loop" << endl << endl;
cout << "[" << s << "]" << endl;
cout << "eof: " << f.eof() << "; fail: " << f.fail() << "; "
<< "bad: " << f.bad() << "; good: " << f.good() << endl;
cout << endl;
return 0;
}

When I run this code, I get the following output:

[A2345]
eof: 0; fail: 0; bad: 0; good: 1

[B23456789]
eof: 0; fail: 0; bad: 0; good: 1

Outside loop

[C23456789]
eof: 0; fail: 1; bad: 0; good: 0

It seems fstream::getline() sets the fail flag to true when the line
encountered is longer than the limit specified. But why does the if ()
( condition evaluate to false?

I couldn't find any information pertaining to the return value of
getline() here: http://stdcxx.apache.org/doc/stdlibref/basic-istream.html#idx173
Is this the right documentation or do I need to look at some other
documentation?
 
J

John H.

It seems fstream::getline() sets the fail flag to true when the line
encountered is longer than the limit specified. But why does the if ()
( condition evaluate to false?

As you have seen, an fstream is designed to be able to evaluate like a
boolean value, where true means healthy and false means problem.
The mechanism you see is that getline returns a reference to the
object it was called on, and then that object gets converted into a
void*. This void* is null on error and non-null when things go ok.
Is this the right documentation or do I need to look at some other
documentation?

You might take a peek at
http://www.cplusplus.com/reference/iostream/fstream/
 
B

Ben Cottrell

John said:
As you have seen, an fstream is designed to be able to evaluate like a
boolean value, where true means healthy and false means problem.
The mechanism you see is that getline returns a reference to the
object it was called on, and then that object gets converted into a
void*. This void* is null on error and non-null when things go ok.

I've always felt that this is a bit of an odd thing for the stream
classes to do, I would think it more sensible to have operator bool
defined instead. is there some subtle reason why operator void* is
used here?
 
J

James Kanze

There was an irrational fear that conversion to bool would
result in coding errors when a stream object was used in a
numeric context. That was followed by an irrational fear that
converting to void* would result in coding errors if someone
tried to delete a stream object.

I'm not sure that the word "irrational" is appropriate in this
context. Historically, bool wasn't available, and the
conversion to int was felt to have some dangers (but of course,
what doesn't?). If the committee had been willing to change it,
a convertion to a pointer to member would seem the safest,
assuming it considered the risks important, and a conversion to
bool the most logical, assuming it didn't consider the risks
that important. Globally, at the committee level, I suspect it
was more a question of not wanting to change something that had
been that way for ages. (But of course, they changed a lot of
other things, and broke a significant amount of code, sometimes
for no benefit whatsoever.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top