- string line
-
ifstream inf("file.txt");
-
while(getline(inf, string))
-
{
-
cout << string << endl;
-
}
一直不明白 getline的返回值是什么, 为什么可以用来 判断
因为 它的原型是
- istream& getline (char* s, streamsize n );
-
istream& getline (char* s, streamsize n, char delim );
是一个引用,引用不会为空,它应该永远为真阿, 这是怎么实现的?问题先记到这里,
等有机会看源码,看它是怎么实现这个函数的
下面是网上摘过来的
istream& getline (istream& is, string& str, char delim = '\n ');
Reads characters from an input stream into a string, stopping when one of the following things happens:
An end-of-file condition occurs on the input stream When the maximum
number of characters that can fit into a string have been read
When a character read in from the string is equal to the specified
delimiter (newline is the default delimiter); the delimiter character is
removed from the input stream, but not appended to the string.
The return value is a reference to the input stream. If
the stream is tested as a logical value (as in an
if or while), it is equivalent to true if the read
was successful and false otherwise (e.g., end of file).
阅读(9125) | 评论(0) | 转发(0) |