我正在尝试将一些标准输出重定向到文本文件,将其他一些重定向到命令提示符。
我目前正在将所有内容输出到文件中,但是我想将它们输出到命令提示符中,因此我至少可以知道(得到一些匹配)记录的内容(因为运行大约需要10分钟)此代码)
这就是我在做什么;
FILE *stream ;
std::stringstream ss;
ss << "K_file.txt";
if((stream = freopen(ss.str().c_str(), "w", stdout)) == NULL)
exit(-1);
std::cout<<"blah blah blah...";
根据评论进行编辑;
“一些”是我想明确指定的示例代码的一部分;
for(int i = 0; i<1000; i++)
{
std::cout<<"I would like this to go to the file - since it's detailed";
}
std::cout<<"loop finished - I would like this to go to the command prompt";
这可能不是最好的例子,但我希望您明白这一点。
最佳答案
您可以为此“滥用”标准输出和标准错误流。例如:
#include <iostream>
void main() {
std::cout << "standard output";
std::cerr << "standard error";
}
现在,如果您redirect仅提交标准错误...
your_program.exe 2> file.txt
...您将在控制台窗口中获得“标准输出”,并在
file.txt
中获得“标准错误”。(注意:这是Windows重定向语法-如果您需要的话,我确定在其他操作系统上进行重定向不会有问题。)