C++ cout 上没有控制台输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4591915/
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 15:51:40  来源:igfitidea点击:

No console output on cout

c++eclipseconsoleflush

提问by Beasly

Good morning,

早上好,

I have a problem with Eclipse IDE for C/C++ Developers.

我对 C/C++ 开发人员的 Eclipse IDE 有问题。

I'm writting a smal tool for converting Strings. While testing on some point eclipse stopped to give console output. e.g.:
cout<<"test";
doesn't get displayed.

我正在编写一个用于转换字符串的小工具。在某些点上进行测试时,eclipse 停止提供控制台输出。例如:
cout<<"test";
不会显示。

But it's not every where... another example:

但这不是每个地方......另一个例子:

// File path as argument
int main(int argc, char* argv[]) {
if (argc != 2) {
    cout
            << "ERROR: Wrong amount of arguments! Only one allowed...\n";
    cout << "\n" << "Programm closed...\n\n";
    exit(1);
}

CommandConverter a(argv[1]);
cout<<"test";
a.getCommandsFromCSV();
cout<<"test2";

return 0;
}

The error message is displayed correctly if the argument is missing. But if the argument is there and the program continues the test outputs:

如果缺少参数,错误消息将正确显示。但是如果参数存在并且程序继续测试输出:

cout<<"test";
cout<<"test2";

cout<<"测试";
cout<<"test2";

are not displayed...
I am missing something obvious?

没有显示......
我错过了一些明显的东西?

Thanks in advance!

提前致谢!

回答by zvrba

You need to end output strings with newline, e.g.: `cout << "test\n"``. The reason is that the standard output is buffered and the buffer is flushed on newline. There probably exists a way to flush the cout buffer without outputting a newline, but I don't know it by heart. Probably includes access to the underlying streambuf (via the rdbuf method).

您需要用换行符结束输出字符串,例如:`cout << "test\n"``。原因是标准输出被缓冲,缓冲区在换行符上刷新。可能有一种方法可以在不输出换行符的情况下刷新 cout 缓冲区,但我不知道。可能包括对底层流缓冲的访问(通过 rdbuf 方法)。

回答by trenki

For me installing the 32 bit versions of Eclipse (Indigo 3.7) and the 32 bit Java JDK/JRE did not work. I use the much quicker solution from the Eclipse CDT/User/FAQ:

对我来说,安装 32 位版本的 Eclipse(Indigo 3.7)和 32 位 Java JDK/JRE 不起作用。我使用Eclipse CDT/User/FAQ 中更快的解决方案:

Quote from Eclipse CDT/User/FAQ - Eclipse console does not show output on Windows:

来自Eclipse CDT/User/FAQ 的引用- Eclipse 控制台在 Windows 上不显示输出

Eclipse console does not show output on Windows In Eclipse CDT on Windows, standard output of the program being run or debugged is fully buffered, because it is not connected to a Windwos console, but to a pipe. See bug 173732 for more details. Either add fflush calls after every printf or add the following lines in the start of the main function:

setvbuf(stdout, NULL, _IONBF, 0); 
setvbuf(stderr, NULL, _IONBF, 0);

Eclipse 控制台不显示 Windows 上的输出 在 Windows 上的 Eclipse CDT 中,正在运行或调试的程序的标准输出是完全缓冲的,因为它没有连接到 Windwos 控制台,而是连接到管道。有关更多详细信息,请参阅错误 173732。在每个 printf 之后添加 fflush 调用或在主函数的开头添加以下行:

setvbuf(stdout, NULL, _IONBF, 0); 
setvbuf(stderr, NULL, _IONBF, 0);

回答by Lachlan

I had a similar problem. In my case the program would give output if run from the command line but not from eclipse console. The solution was to use the 32 bit version of eclipse and not the 64 bit one.

我有一个类似的问题。在我的情况下,如果从命令行而不是从 eclipse 控制台运行,程序会给出输出。解决方案是使用 32 位版本的 eclipse 而不是 64 位版本。

I read that it was a bug. Might not be the same issue though.

我读到这是一个错误。不过可能不是同一个问题。

回答by javed

I was also searching for exactly this information when I found this on the Microsoft website http://support.microsoft.com/kb/94227

当我在 Microsoft 网站http://support.microsoft.com/kb/94227上找到此信息时,我也在搜索此信息

I think a simple method is to use std::flush when you want to force flushing the internal buffer that cout uses

我认为一个简单的方法是当你想强制刷新 cout 使用的内部缓冲区时使用 std::flush

*std::cout << ... << std::flush;*

回答by Puneet Singh

This Happens when you debug your code and dont see the output till the last. use

当您调试代码并且直到最后才看到输出时,就会发生这种情况。用

cout<<"what ever overloads"<< flush;

to see the output immediately on stdout(console)

立即在 stdout(console) 上查看输出

回答by somebody

Hi after some similar struggle I figured out, that the first element of the project's properties environment PATH variable must be "C:\MinGW\bin;" Otherwise a wrong version might be used, especially if you use different compiler.

嗨,经过一些类似的斗争,我发现,项目属性环境 PATH 变量的第一个元素必须是“C:\MinGW\bin;” 否则可能会使用错误的版本,尤其是当您使用不同的编译器时。

回答by Chris Seddon

try outputting a space at the beginning of each line

尝试在每一行的开头输出一个空格

cout << " " << .....

cout << " " << .....