急:使用cout.rdbuf()重定向的问题

lion7beck 2018-08-29 09:27:09
如下的函数,可以正常打印出“Testn”(在out.txt中)和“Test2n”(在屏幕上)的内容。
但移除掉第10行——即“cout.rdbuf(x);”的内容,打算把“Test2n”也重定向到out.txt中,就出现了“段错误 (核心已转储)”的错误提示,环境为Ubuntu12。
感谢指导!

#include <iostream>
#include <fstream>

using namespace std;
int main(void)
{
ofstream outf("out.txt");
streambuf* x = cout.rdbuf(outf.rdbuf());// redirect to out.txt
cout << "Testn"<<endl; // write to out.txt
cout.rdbuf(x); // recovery
cout << "Test2n"<<endl; // write to screen
return 0;
}
...全文
637 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
棉猴 2018-08-30
  • 打赏
  • 举报
回复
mSDN上的例子跟你的代码一样啊,应该没问题的

ofstream file("rdbuf.txt");
streambuf *x = cout.rdbuf(file.rdbuf());
cout << "test"<<endl;
cout.rdbuf(x);
cout<<"test2"<<endl;

在主进程应该可以重定向子进程中的stdout,只要保证子进程和主进程中使用的是同一个变量
lion7beck 2018-08-30
  • 打赏
  • 举报
回复
兄弟,非常感谢!你提供的代码是OK的
再请教下:
如果我在主进程用popen()调用了另一个子进程,想在主进程中把子进程的stdout重定向到out.txt文件中,能做到吗?

引用 3 楼 sghcpt 的回复:
楼主,你上面的代码在windows下vs2013控制台工程上运行没有报错,也能把数据写入到out.txt文件中。根据官方例子,修改了如下代码,楼上看看在你那边行不行?
#include <iostream>
#include <fstream>

using namespace std;
int main(void)
{
ofstream outf("out.txt");
streambuf* backup = cout.rdbuf();

cout.rdbuf(outf.rdbuf());

cout << "Testn" << endl;
cout << "Test2n" << endl;
cout << "Test3n" << endl;
cout << "Test4n" << endl;
cout << "Test5n" << endl;

cout.rdbuf(backup);
outf.close();
return 0;
}
sghcpt 2018-08-30
  • 打赏
  • 举报
回复
楼主,你上面的代码在windows下vs2013控制台工程上运行没有报错,也能把数据写入到out.txt文件中。根据官方例子,修改了如下代码,楼上看看在你那边行不行?
#include <iostream>
#include <fstream>

using namespace std;
int main(void)
{
ofstream outf("out.txt");
streambuf* backup = cout.rdbuf();

cout.rdbuf(outf.rdbuf());

cout << "Testn" << endl;
cout << "Test2n" << endl;
cout << "Test3n" << endl;
cout << "Test4n" << endl;
cout << "Test5n" << endl;

cout.rdbuf(backup);
outf.close();
return 0;
}

lion7beck 2018-08-29
  • 打赏
  • 举报
回复



引用 1 楼 lion7beckham 的回复:

#include <iostream>
#include <fstream>

using namespace std;
int main(void)
{
ofstream outf("out.txt");
streambuf* x = cout.rdbuf(outf.rdbuf());// redirect to out.txt
cout << "Testn"<<endl; // write to out.txt
// cout.rdbuf(x); // recovery
cout << "Test2n"<<endl;
cout << "Test3n"<<endl;
cout << "Test4n"<<endl;
cout << "Test5n"<<endl;
//Test2n~Test5n尽管也打印到out.txt中了,但是执行完之前报了个“段错误 (核心已转储)”的错误。。
return 0;
}

这是出错的代码,因为我要打印很多内容到out.txt中。
lion7beck 2018-08-29
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>

using namespace std;
int main(void)
{
ofstream outf("out.txt");
streambuf* x = cout.rdbuf(outf.rdbuf());// redirect to out.txt
cout << "Testn"<<endl; // write to out.txt
// cout.rdbuf(x); // recovery
cout << "Test2n"<<endl;
cout << "Test3n"<<endl;
cout << "Test4n"<<endl;
cout << "Test5n"<<endl;
//Test2n~Test5n尽管也打印到out.txt中了,但是执行完之前报了个“段错误 (核心已转储)”的错误。。
return 0;
}

65,189

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧