将 cout 重定向到一个文件
redirect cout to a file
我想在需要时将 cout 重定向到一个文件。这是我的测试程序的主要部分,但我无法确定某些输入未定向到该文件。
ucla();
ucla() {
std::cout << "inside ucla" << std::endl;
}
int main() {
const char* outName = argv[2];
std::string outFile(outName);
std::streambuf *coutbuf, *termbuf;
termbuf = std::cout.rdbuf();
if (!outFile.empty()) {
std::ofstream outstr;
outstr.open(argv[2]);
coutbuf = outstr.rdbuf();
std::cout.rdbuf(coutbuf);
std::cout << "here" << std::endl;
} else {std::cout.rdbuf(termbuf);}
std::cout << "this file " << std::endl;
ucla();
}
当程序是运行时,只有这里被写入文件,我希望
这个文件
加州大学洛杉矶分校内
我搜索了较早的线程,但无法弄清楚缺少什么。谢谢
您的程序有未定义的行为。
coutbuf
在outstr
被销毁时被销毁。由于在 if
块结束时您没有重置 cout
的 rdbuf
,因此 cout
指向 if
之外的悬空 rdbuf
-块。
清理建议。始终为函数提供 return 类型。而不是
ucla();
使用
void ucla();
更新,回应 OP 的评论
要解决此问题,最好将所有 "application code" 移动到另一个函数并从 main
调用该函数。将 rdbuf
处理代码留在 main
.
中
void ucla()
{
std::cout << "inside ucla" << std::endl;
}
void application_main()
{
std::cout << "here" << std::endl;
std::cout << "this file " << std::endl;
ucla();
}
int main()
{
const char* outName = argv[2];
std::string outFile(outName);
// Save the rdbuf of cout.
std::streambuf* coutbuf = std::cout.rdbuf();
termbuf = std::cout.rdbuf();
if (!outFile.empty())
{
std::ofstream outstr;
outstr.open(argv[2]);
std::cout.rdbuf(outstr.rdbuf());
// Needs to be here so that every use of cout will redirect the output
// to the file.
application_main();
}
else
{
// In this branch, use of cout will produce output in the console.
application_main();
}
// Restore the rdbuf of cout.
std::cout.rdbuf(coutbuf);
}
我想在需要时将 cout 重定向到一个文件。这是我的测试程序的主要部分,但我无法确定某些输入未定向到该文件。
ucla();
ucla() {
std::cout << "inside ucla" << std::endl;
}
int main() {
const char* outName = argv[2];
std::string outFile(outName);
std::streambuf *coutbuf, *termbuf;
termbuf = std::cout.rdbuf();
if (!outFile.empty()) {
std::ofstream outstr;
outstr.open(argv[2]);
coutbuf = outstr.rdbuf();
std::cout.rdbuf(coutbuf);
std::cout << "here" << std::endl;
} else {std::cout.rdbuf(termbuf);}
std::cout << "this file " << std::endl;
ucla();
}
当程序是运行时,只有这里被写入文件,我希望 这个文件 加州大学洛杉矶分校内 我搜索了较早的线程,但无法弄清楚缺少什么。谢谢
您的程序有未定义的行为。
coutbuf
在outstr
被销毁时被销毁。由于在 if
块结束时您没有重置 cout
的 rdbuf
,因此 cout
指向 if
之外的悬空 rdbuf
-块。
清理建议。始终为函数提供 return 类型。而不是
ucla();
使用
void ucla();
更新,回应 OP 的评论
要解决此问题,最好将所有 "application code" 移动到另一个函数并从 main
调用该函数。将 rdbuf
处理代码留在 main
.
void ucla()
{
std::cout << "inside ucla" << std::endl;
}
void application_main()
{
std::cout << "here" << std::endl;
std::cout << "this file " << std::endl;
ucla();
}
int main()
{
const char* outName = argv[2];
std::string outFile(outName);
// Save the rdbuf of cout.
std::streambuf* coutbuf = std::cout.rdbuf();
termbuf = std::cout.rdbuf();
if (!outFile.empty())
{
std::ofstream outstr;
outstr.open(argv[2]);
std::cout.rdbuf(outstr.rdbuf());
// Needs to be here so that every use of cout will redirect the output
// to the file.
application_main();
}
else
{
// In this branch, use of cout will produce output in the console.
application_main();
}
// Restore the rdbuf of cout.
std::cout.rdbuf(coutbuf);
}