cout 上没有控制台输出
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要以换行符结束输出字符串,例如:`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).
对于我来说,安装 32 位版本的 Eclipse (Indigo 3.7) 和 32 位 Java JDK/JRE 不起作用。我使用 Eclipse CDT/User/FAQ 中更快的解决方案:
引用 Eclipse CDT/用户/常见问题解答 - Eclipse 控制台在 Windows 上不显示输出:
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 控制台运行。解决方案是使用 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.
当我在 Microsoft 网站上找到此信息时,我也在搜索此信息
http://support.microsoft.com/kb/94227
我认为一个简单的方法是使用std::flush 当您想要强制刷新 cout 使用的内部缓冲区时
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
当您调试代码并且直到最后才看到输出时,就会发生这种情况。
用于
立即在 stdout(控制台)上查看输出
This Happens when you debug your code and dont see the output till the last.
use
to see the output immediately on stdout(console)
嗨,经过一些类似的斗争,我发现,项目属性环境 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.
尝试在每行的开头输出一个空格
cout << ” “<< ……
try outputting a space at the beginning of each line
cout << " " << .....