cout 上没有控制台输出

发布于 2024-10-10 09:19:54 字数 724 浏览 8 评论 0原文

我在使用 Eclipse IDE for C/C++ Developers 时遇到问题。

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

但并非所有地方都如此......另一个例子:

// 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;
}

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

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

不显示...
我错过了一些明显的东西吗?

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

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.

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";

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

请叫√我孤独 2024-10-17 09:19:54

您需要以换行符结束输出字符串,例如:`cout << “测试\n”``。原因是标准输出被缓冲并且缓冲区在换行符上刷新。可能存在一种方法可以刷新 cout 缓冲区而不输出换行符,但我不记得它。可能包括对底层streambuf的访问(通过rdbuf方法)。

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

初见 2024-10-17 09:19:54

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

引用 Eclipse CDT/用户/常见问题解答 - Eclipse 控制台在 Windows 上不显示输出

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

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

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:

Quote from Eclipse CDT/User/FAQ - Eclipse console does not show output on 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);
一桥轻雨一伞开 2024-10-17 09:19:54

我有类似的问题。就我而言,如果从命令行运行该程序将给出输出,但不是从 Eclipse 控制台运行。解决方案是使用 32 位版本的 eclipse 而不是 64 位版本。

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

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.

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

傲影 2024-10-17 09:19:54

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

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

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

I was also searching for exactly this information when I found this on the Microsoft website
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

*std::cout << ... << std::flush;*
飘过的浮云 2024-10-17 09:19:54

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

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

立即在 stdout(控制台)上查看输出

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)

没有你我更好 2024-10-17 09:19:54

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

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.

め七分饶幸 2024-10-17 09:19:54

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

cout << ” “<< ……

try outputting a space at the beginning of each line

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

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