istream::getline 返回类型
istream::getline
方法返回什么?
我问这个问题是因为我看到要循环遍历文件,应该这样做:
while ( file.getline( char*, int ) )
{
// handle input
}
返回什么?
What does the istream::getline
method return?
I am asking because I have seen that to loop through a file, it should be done like this:
while ( file.getline( char*, int ) )
{
// handle input
}
What is being returned?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它返回一个流,以便我们可以链接操作。
但是,当您在布尔上下文中使用对象时,编译器会查找可以将其转换为可在布尔上下文中使用的类型的转换运算符。
C++11
在这种情况下,流具有
显式运算符 bool() const
。调用时它会检查错误标志。如果设置了failbit 或badbit,则返回false,否则返回true。C++03
在这种情况下,流具有
operator void*() const
。由于这会产生一个指针,因此可以在布尔上下文中使用它。调用时它会检查错误标志。如果设置了failbit或badbit,那么它返回NULL,这相当于FALSE,否则它返回一个指向self的指针(或者其他有效的东西,尽管你不应该使用这个事实))。用法
因此,您可以在任何需要布尔测试的上下文中使用流:
注意:在外部测试流,然后在条件/循环语句体内对其进行读/写是个坏主意。这是因为读取行为可能会使流变差。通常最好将阅读作为测试的一部分进行。
It returns a stream so that we can chain the operation.
But when you use an object in a boolean context the compiler looks for an conversion operator that can convert it into a type that can be used in the boolean context.
C++11
In this case stream has
explicit operator bool() const
. When called it checks the error flags. If either failbit or badbit are set then it returns false otherwise it returns true.C++03
In this case stream has
operator void*() const
. As this results in a pointer it can be used in a boolean context. When called it checks the error flags. If either failbit or badbit are set then it returns NULL which is equivalent to FALSE otherwise it returns a pointer to self (or something else valid though you should not use this fact)).Usage
So you can use a stream in any context that would require a boolean test:
Note: It is bad idea to test the stream on the outside and then read/write to it inside the body of the conditional/loop statement. This is because the act of reading may make the stream bad. It is usually better to do the read as part of the test.
它返回流本身。流可以(通过
void*
)转换为bool
来指示其状态。在此示例中,当流转换为bool
变为“false”时(当流进入错误状态时会发生这种情况),您的while
循环将终止。在您的代码中,当尝试读取文件末尾之后的内容时,最有可能发生这种情况。简而言之,它会读取尽可能多的内容,然后停止。It returns the stream itself. The stream can convert (through
void*
) tobool
indicating its state. In this example, yourwhile
loop will terminate when the stream's conversion tobool
goes "false", which happens when your stream enters an error state. In your code, it's most likely to occur when there was an attempt to read past the end of the file. In short, it'll read as much as there is, and then stop.请参阅参考。
getline
返回的istream通过隐式转换< /a> 检查操作是否成功。该转换使if(mystream.getline(a,b))
的使用成为if(!mystream.getline(a,b).fail())
的简写形式。Look from reference. The istream returned from
getline
is converted to bool by implicit conversion to check success of operation. That conversion makes usage ofif(mystream.getline(a,b))
into shorthand forif(!mystream.getline(a,b).fail())
.该函数返回对流对象本身的引用,该引用可用于链接进一步的读取操作:
或者,因为流可以在循环或条件中隐式转换为
void *
:您可以读取有关此内容的更多信息,请访问 cplusplus.com 网站:
http://cplusplus.com/reference/iostream/ istream/getline/
The function returns a reference to the stream object itself, which can be used either to chain further read operations:
or, because streams are implicitly convertible to
void *
s, in a loop or condition:You can read more about this on the cplusplus.com website:
http://cplusplus.com/reference/iostream/istream/getline/
每个人都告诉过你它是什么,现在让我告诉你,使用自由形式版本
为什么是这个版本?它应该立即变得显而易见 - 您传递的是
std::string
而不是一些固定大小的字符缓冲区!Everyone has told you what it is, now let me tell you, use the free form version
Why this version? It should become immediately apparent - you pass in a
std::string
rather than some fixed size character buffer!