C++ istream::getline 返回类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4708335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:14:06  来源:igfitidea点击:

istream::getline return type

c++file-ioifstreamgetlineistream

提问by Martin York

What does the istream::getlinemethod return?

什么是istream::getline方法的返回?

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?

什么被退回?

回答by Martin York

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

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.

在这种情况下,流具有explicit operator bool() const. 当被调用时,它会检查错误标志。如果设置了failbit 或badbit,则返回false,否则返回true。

C++03

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)).

在这种情况下,流具有operator void*() const. 由于这会产生一个指针,因此它可以在布尔上下文中使用。当被调用时,它会检查错误标志。如果设置了 failbit 或 badbit 则它返回 NULL ,这等效于 FALSE 否则它返回一个指向 self 的指针(或其他有效的东西,尽管你不应该使用这个事实))。

Usage

用法

So you can use a stream in any context that would require a boolean test:

因此,您可以在需要布尔测试的任何上下文中使用流:

if (stream >> x)
{    
}

while(stream)
{
    /* do Stuff */
}

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.

注意:在外部测试流然后在条件/循环语句的主体内部对其进行读/写是个坏主意。这是因为阅读行为可能会使流变坏。通常最好将阅读作为测试的一部分。

while(std::getline(steam, line))
{
    // The read worked and line is valid.
}

回答by ?? Tiib

Look from reference. The istream returned from getlineis converted to bool by implicit conversionto check success of operation. That conversion makes usage of if(mystream.getline(a,b))into shorthand for if(!mystream.getline(a,b).fail()).

参考看。从返回的 istreamgetline通过隐式转换转换为 bool以检查操作是否成功。这种转换使用if(mystream.getline(a,b))into 简写为if(!mystream.getline(a,b).fail()).

回答by templatetypedef

The function returns a reference to the stream object itself, which can be used either to chain further read operations:

该函数返回对流对象本身的引用,可用于链接进一步的读取操作:

myStream.getline(...).getline(...);

or, because streams are implicitly convertible to void *s, in a loop or condition:

或者,因为流可以void *在循环或条件中隐式转换为s:

while (myStream.getline(...)) {
    ...
}

You can read more about this on the cplusplus.com website:

您可以在 cplusplus.com 网站上阅读更多相关信息:

http://cplusplus.com/reference/iostream/istream/getline/

http://cplusplus.com/reference/iostream/istream/getline/

回答by Lightness Races in Orbit

It returns the stream itself. The stream can convert (through void*) to boolindicating its state. In this example, your whileloop will terminate when the stream's conversion to boolgoes "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.

它返回流本身。流可以转换(通过void*)以bool指示其状态。在此示例中,while当流转换为bool“false”时,您的循环将终止,当您的流进入错误状态时会发生这种情况。在您的代码中,当尝试读取文件末尾时最有可能发生这种情况。简而言之,它会尽可能多地读取,然后停止。

回答by Nim

Everyone has told you what it is, now let me tell you, use the free formversion

每个人都告诉你它是什么,现在让我告诉你,使用自由格式版本

std::string line; 
while(getline(file, line)) // assuming file is an instance of istream
{
//
}

Why this version? It should become immediately apparent - you pass in a std::stringrather than some fixed size character buffer!

为什么是这个版本?它应该立即变得明显 - 您传入的是一个std::string而不是一些固定大小的字符缓冲区!