C++ 第一个 cout 不会被打印出来

C++ the first cout will not be printed

如您所见,我在循环开始之前在这里进行了一次计算。 seekg 从头开始​​,txt 文件中的第一个单词是 "Hello"。但它不会打印出来(第一个 cout)。

当我删除 while 循环时,这不是问题,第一个单词会包含在输出中。

第一个"cout",不是应该不管后面的都打印出来吗?下一行代码(这种情况)怎么可能影响它上面的前一个"cout"?

 int main()
 {
    string a;
    ifstream myfile;

    myfile.open("test.txt");
    myfile.seekg(0);

    getline(myfile,a);
    cout << a << endl;

    int z = 0;
    while( a != "x" ) {
        myfile.seekg(z);
        getline(myfile,a);
        z += a.size() + 2;
        cout << a << endl;
    }
}

我还应该提到该文件以 "hello" 开头,并且包含 600 个换行符和每个换行符上的一个单词。最后一行包含 "x".

[编辑]

这是另一个例子。我相信这会有所帮助。这是我的代码:

int main() {

        string a;
        ifstream myfile;
        int newpos = 0;
        myfile.open("example.txt");
        myfile.seekg(newpos);

        getline(myfile,a);


        while (a != "x") {

           myfile.seekg(newpos);

           getline(myfile,a);

           cout << a << endl;

           newpos += a.size() + 2;

        }

}

这是我的文本文件:

hello
johan
nils
erik
john
x

输出与上面的文本文件中的完全一样。(到目前为止还不错)。

但是,然后我将大约 600 个新名称添加到 list/textfile(与上面的结构相同,没有更改,只是添加了新名称)。顶部只有一个 "hello",所以我可以跟踪开始,最后一个 x。

这仍然适用于大约 100 个新名称,hello 仍然在开头打印出来。但是当我添加大约 600 个新名字时,你好将不包括在内(第一个 cout)。这是为什么?

But when I add around 600 new names, hello will not be included (the very first cout).
Why is that?

看起来您的 Screen Buffer Size 不够大,无法显示所有缓冲行。因此,当打印的行数超过特定高度时,控制台开始 忽略 之前打印的行,这导致您认为它没有打印所有行。

由于您正在使用 Windows,您可以尝试增加它

对于所有控制台应用程序

  • Right-click 控制台的标题并选择 Defaults
  • 转到布局选项卡
  • 在屏幕缓冲区大小下,将 Height 增加到 (i.e 9001)
  • 点击确定然后重启控制台

针对特定应用程序

  • Right-click 控制台的标题并选择 Properties
  • 转到布局选项卡
  • 在屏幕缓冲区大小下,将 Height 增加到 (i.e 9001)
  • 单击确定

请注意,大多数 Windows 操作系统的默认高度为 9001