gpt4 book ai didi

c++ - ofstream - 我可以将 "cout"重定向到文件中吗?

转载 作者:太空宇宙 更新时间:2023-11-04 14:44:28 24 4
gpt4 key购买 nike

我只是想知道你是否有办法做到这一点:

ofstream exemple (name);
exemple << Display();

Display() 是一个 void 方法,只能做类似的事情:

cout << "Something" << endl;

我会这样做,因为我已经为每个类编写了所有方法 Display(),我想将它们发送到 cout 的内容放入我的文件中,或者重新创建一些方法“string Display()”。

这可能吗?

谢谢!马可

最佳答案

You can change cout's buffer , 但 cout 是一个全局变量,因此会影响整个程序。

为什么不让 Display() 接收输出流作为参数?

void Display(std::ostream &cout) {
cout << "Something" << endl;
}

关于c++ - ofstream - 我可以将 "cout"重定向到文件中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21064584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com