Hello. I've been using the different forms of std::getline() in a program recently, and I can't help but notice one strange thing about how it sets error flags. I'm reading in some data using
And it seems that if the stream reaches num chars read before a delim, it sets the fail bit. Here's a little test program using ifstream.Code:istream& istream::getline(char*buf, int num, char delim = '\n')
And my perplexing output:Code:#include <iostream> #include <cstdlib> #include <fstream> using namespace std; int main(int argc, char *argv[]) { ifstream group("c:\\windows\\desktop\\input.txt"); if(!group) { cout << "can't open." << endl; return EXIT_FAILURE; } char *aunt_sally = new char[25]; cout << boolalpha << "before getline: " << group.fail() << endl; group.getline(aunt_sally,25,'@'); cout << "after getline: " << group.fail() << endl; cout << aunt_sally << endl; group.close(); system("PAUSE"); return EXIT_SUCCESS; } /********input.txt*************** This is a text-only test just to see if the damn thing works. Of course, as I write this, I haven't even compiled all of my new functions. It is likely that I will get tons of error and not even use this test file for quite a while. Still, I'm not discouraged. (quite more than 25 chars) *********************************/
Shouldn't it be "after getline: false" ? I even tried it with a '@' right there at char 25, and then the fail bit was not set. Is this normal behavior?Code:before getline: false after getline: true This is a text-only test Press any key to continue . . .



LinkBack URL
About LinkBacks


