我正在尝试使用cerr
和clog
而不是cout
来发出错误消息。但我遇到了一个问题。下面是一个简化的版本。
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; ++i) {
int x;
cin >> x;
cout << "first" << endl;
cerr << "second" << endl;
}
return 0;
}
但结果是
1
first
second
2
second
first
3
second
first
4
first
second
5
second
first
但是,如果我在win10的cmd上使用g++来编译和运行,就可以了。
1
first
second
2
first
second
3
first
second
4
first
second
5
first
second
因此,我开始怀疑是否有一些特定的设置使得Clion内部编译和运行环境与g++不同。但我不确定。有人能告诉我为什么吗?
发布于 2020-12-02 22:02:31
在引擎盖下有很多东西(SPD在评论中提到了一些,但不是全部-中间有一个pty-管道)。但这只是工作:
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; ++i) {
int x;
cin >> x;
cout.flush();
cerr.flush();
cout << "first" << endl;
cout.flush();
cerr << "second"<< endl;
cerr.flush();
}
return 0;
}
阅读更多here。
https://stackoverflow.com/questions/65110558
复制相似问题