【发布时间】:2014-11-29 17:40:52
【问题描述】:
是否可以在文件上显示 cout 输出而不是在控制台/终端中显示?
#include <iostream>
#include <fstream>
void showhello()
{
cout << "Hello World" << endl;
}
int main(int argc, const char** argv)
{
ofstream fw;
fw.open("text.txt");
fw << showhello() << endl;
}
如果我简单地把 cout
限制: 假设函数 showhello() 包含一千个 cout 输出,所以你不能使用类似的东西:
fw << "Hello World" << endl;
或复制粘贴到字符串中。它必须是 fw
【问题讨论】:
-
据我所知,您正在做的事情应该可以正常工作。
-
您最好重构
showHello以将ostream作为参数并输出到该参数。您可以选择提供不带参数的showHello重载调用传入std::cout的重构版本。
标签: c++ file function void cout