【发布时间】:2018-04-20 09:11:31
【问题描述】:
我正在尝试将以下整个函数写入文本文件,同时仍保持其控制台输出功能而没有代码冗余。有没有一种简单的方法可以将整个方法的结果同时发布到文件和控制台?
#include<iostream>
#include<fstream>
void sports(){
cout<<"\nGame_Code\t\tGane\t\tCost\t\tStart Time\n";
cout<<"\nSP-9651\t\t Game 1 \t\t60\t\t08:00";
cout<<"\nSP-9652\t\t Game 2 \t\t60\t\t09:15";
cout<<"\nSP-9653\t\t Game 3 \t\t55\t\t09:55";
cout<<"\nSP-9654\t\t Game 4 \t\t55\t\t11:00";
cout<<"\nSP-9655\t\t Game 5 \t\t50\t\t13:00";
cout<<"\nSP-9657\t\t Game 7 \t\t45\t\t19:45";
cout<<"\nSP-9659\t\t Game 8 \t\t70\t\t22:45";
cout<<"\n\n";
}
int main(){
//This is for console output
sports();
}
【问题讨论】:
-
见Copy, load, redirect and tee using C++ streambufs。不过,处理 c++ 流缓冲区并不简单。
-
@FredrickChege VTT 中的链接绝对是您需要的。您感兴趣的部分是 Tee 流。