讨论个问题:C++中成员函数版本的getline问题
C++标准解释是:
istream::getline public member function
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
设置eofbit:
The end of the source of characters is reached during its operations.
设置failbit:
No characters were extracted because the end was prematurely found.
This is also set if the function stops extracting because n-1 characters were extracted (n including the terminating null-character).
Notice that some eofbit cases will also set failbit.
如果函数从输入流中提取了n-1个字符的话,那么failbit会被设置。
但我测试后,发现并没有设置failbit。
比如,test.txt里有二行Press,fstream fs; char s[6];
调用fs.getline(s,6);
cout<<fs.gcount()为6;也就是说已经从文件流中提取出了6个字符,大于5。应该设置failbit,但实际并没有!!
如果将参数6改为小一点的数,failbit会被设置。
我的理解是:如果文件中一行有m个字符(默认delimiter为'\n'),n的值至少必须为m+1,这样提取出一行后,可以在最后加上'\0',而且gcount()为m+1,m个字符加上一个换行'\n'。文件流的failbit也不会被设置,否则会被设置。
但这与标准不符啊!!
望达人予以指点!!