我正在尝试寻找一种将fout或cout传递给函数的方法。我意识到有逻辑上简单的处理方式,比如在任何输出数据的函数中放置if语句,甚至只是编写两种功能的函数。但是,这似乎是原始和低效的。我不相信这个代码会起作用,我把它放在这里只是为了确保很容易看到我想做什么。请注意,我正在使用C++算法设计课程学习,我并不是一个经验丰富的C++程序员。我的课程限制了使用您所看到的头文件。
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void helloWorld(char);
ofstream fout;
int main()
{
fout.open("coutfout.dat");
helloWorld(c);
helloWorld(f);
return 0;
}
void helloWorld(char x)
{
xout << "Hello World";
return;
}
stream
参数...std::ostream& sayHello(std::ostream& stream){return stream << "Hello World";}
- GingerPlusPlus