查找cin的流的末尾&;ifstream? 我正在通过一本C++的教科书来运行我自己,这是我对C++编程的一个补充。其中一个实践问题(没有太多细节)希望我定义一个可以通过ifstream或cin(例如istream)作为参数传递的函数。从那里,我必须阅读这条小溪。问题是,我无法找到一种方法让这个函数使用cin和ifstream来有效地找到流的结尾。即 while(input_stream.peek() != EOF)

查找cin的流的末尾&;ifstream? 我正在通过一本C++的教科书来运行我自己,这是我对C++编程的一个补充。其中一个实践问题(没有太多细节)希望我定义一个可以通过ifstream或cin(例如istream)作为参数传递的函数。从那里,我必须阅读这条小溪。问题是,我无法找到一种方法让这个函数使用cin和ifstream来有效地找到流的结尾。即 while(input_stream.peek() != EOF),c++,ifstream,eof,cin,istream,C++,Ifstream,Eof,Cin,Istream,不会为cin工作的。我可以重新编写函数以查找某个短语(如“#流结束#”或其他什么),但我认为如果我传递的文件流中有这个确切的短语,这是一个坏主意 我曾想过使用函数重载,但到目前为止,这本书已经提到了何时需要我这样做。我可能在这个练习问题上投入了太多的精力,但我喜欢这个创造性的过程,并且好奇是否有这样一种方法可以在不过载的情况下完成这项工作。为什么std::cin.eof()不起作用cin将发出EOF信号,当用户用Ctrl+d(*nix)或Ctrl+z(Windows)向其发出信号时,或者(在管道

不会为cin工作的。我可以重新编写函数以查找某个短语(如“#流结束#”或其他什么),但我认为如果我传递的文件流中有这个确切的短语,这是一个坏主意


我曾想过使用函数重载,但到目前为止,这本书已经提到了何时需要我这样做。我可能在这个练习问题上投入了太多的精力,但我喜欢这个创造性的过程,并且好奇是否有这样一种方法可以在不过载的情况下完成这项工作。

为什么
std::cin.eof()
不起作用<当stdin关闭时,code>cin将发出EOF信号,当用户用Ctrl+d(*nix)或Ctrl+z(Windows)向其发出信号时,或者(在管道输入流的情况下)当管道文件结束时,会发生EOF()

EOF()
对cin有效。你做错了什么事;请张贴您的密码。一个常见的障碍是
eof
标志在您尝试在流结束后读取后设置

下面是一个演示:

#include <iostream>
#include <string>

int main( int, char*[] )
{
    std::string s;
    for ( unsigned n = 0; n < 5; ++n )
    {
        bool before = std::cin.eof();
        std::cin >> s;
        bool after = std::cin.eof();
        std::cout << int(before) << " " << int(after) << "  " << s << std::endl;
    }

    return 0;
}

(可以在Windows上使用Ctrl-Z,在许多其他操作系统上使用Ctrl-D生成EOF)

如果在布尔上下文中使用流,则它将自身转换为一个值,如果未达到EOF,则该值与true值相等;如果试图读取超过EOF,则转换为false值(如果以前从流中读取时出错,则也为false)

由于流上的大多数IO操作都返回流(因此它们可以链接)。您可以执行读取操作并在测试中使用结果(如上所述)

因此,一个从流中读取数字流的程序:

int main()
{
   int x;

   // Here we try and read a number from the stream.
   // If this fails (because of EOF or other error) an internal flag is set.
   // The stream is returned as the result of operator>>
   // So the stream is then being used in the boolean context of the while()
   // So it will be converted to true if operator>>  worked correctly.
   //                         or false if operator>> failed because of EOF
   while(std::cin >> x)
   {
       // The loop is only entered if operator>> worked correctly.
       std::cout << "Value: " << x << "\n";
   }

   // Exit when EOF (or other error).
}
intmain()
{
int x;
//在这里,我们尝试从流中读取一个数字。
//如果失败(由于EOF或其他错误),将设置一个内部标志。
//该流作为运算符>>的结果返回
//因此,流随后将在while()的布尔上下文中使用
//因此,如果运算符>>工作正常,它将转换为true。
//如果操作员>>因EOF而失败,则为false
而(标准::cin>>x)
{
//只有操作员>>工作正常时,才会进入循环。

std::不能检查这个问题:在Windows中,它是
Ctrl+Z
Ctrl+D
是用于基于UNIX的系统。啊,这确实有效(无论如何,Windows的Ctrl+Z都很好)。对不起,如果有一点混乱,就像我在之前的帖子中(!input_stream.eof())的while(input_stream.peek()!=eof)中最初遇到的一样.无论如何,我对使用while(!input_stream.eof())有一个顾虑当函数读取EOF字符时,会为输入流设置失败位。这应该发生吗?@user435219:是的,因为它读取数据失败,因为它到达了EOF。EOF几乎总是触发
fail
以及抱歉,我最初在(!input_stream.EOF())时遇到了困惑但后来意识到我写的是while(input_stream.peek!=EOF)。无论如何,这两种方法都与control+z一起工作(讽刺的是,我刚刚在维基百科中读到了EOF字符)。感谢atzz和所有人的帮助!宁愿将流转换为布尔值而不是
.EOF
.bad
int main()
{
   int x;

   // Here we try and read a number from the stream.
   // If this fails (because of EOF or other error) an internal flag is set.
   // The stream is returned as the result of operator>>
   // So the stream is then being used in the boolean context of the while()
   // So it will be converted to true if operator>>  worked correctly.
   //                         or false if operator>> failed because of EOF
   while(std::cin >> x)
   {
       // The loop is only entered if operator>> worked correctly.
       std::cout << "Value: " << x << "\n";
   }

   // Exit when EOF (or other error).
}