istream::getline 返回类型

发布于 2024-10-12 00:46:03 字数 180 浏览 8 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

舂唻埖巳落 2024-10-19 00:46:03

它返回一个流,以便我们可以链接操作。

但是,当您在布尔上下文中使用对象时,编译器会查找可以将其转换为可在布尔上下文中使用的类型的转换运算符。

C++11

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

C++03

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

用法

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

if (stream >> x)
{    
}

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

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

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

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:

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(stream, line))
{
    // The read worked and line is valid.
}
怪我太投入 2024-10-19 00:46:03

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

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

甜心小果奶 2024-10-19 00:46:03

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

夜还是长夜 2024-10-19 00:46:03

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

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

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

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

您可以读取有关此内容的更多信息,请访问 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:

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

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

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

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

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

一袭白衣梦中忆 2024-10-19 00:46:03

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

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

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

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

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::string rather than some fixed size character buffer!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文