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?
#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?