【发布时间】:2015-11-18 04:35:48
【问题描述】:
#include <iostream>
#include <ostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
void GetOutputFileStream(std::ofstream * fout, std::string filename);
void PrintStatistics(std::ostream & fout,
int numUsed,
int numNew,
double newTotalPrice,
double newTotalMileage,
double usedTotalPrice,
double usedTotalMileage);
int main()
{
double newTotalPrice = 33333;
double newTotalMileage = 44444;
double usedTotalPrice = 22222;
double usedTotalMileage = 99999;
int numUsed = 2;
int numNew = 3;
std::ofstream fout; // 'f'ile out - fout
std::string filename = "statistics.txt";
GetOutputFileStream(&fout, filename);
// Print to screen
PrintStatistics(std::cout,
numUsed,
numNew,
newTotalPrice,
newTotalMileage,
usedTotalPrice,
usedTotalMileage);
// Print to file
PrintStatistics(fout,
numUsed,
numNew,
newTotalPrice,
newTotalMileage,
usedTotalPrice,
usedTotalMileage);
std::cout << "Press ENTER to continue";
std::cin.get();
return 0;
}
void GetOutputFileStream(std::ofstream * fout, std::string filename)
{
fout->open(filename, std::ios::out);
}
void PrintStatistics(std::ostream & fout,
int numUsed,
int numNew,
double newTotalPrice,
double newTotalMileage,
double usedTotalPrice,
double usedTotalMileage)
{
}
我真的陷入了作业的这个特定部分,它要求我将简单的文本打印到控制台中,然后创建一个文件(无论 std::string 文件名可能在 main 中)并打印控制台的内容进入那个文件。
我真的很困惑,因为该函数需要 ostream 并且还需要该函数以任何文件名进行操作(在此示例中为 statistics.txt,只是为了测试该函数是否正常工作)。
函数是 PrintStatistics。
我知道我可以使用 cout 打印到控制台,然后我假设 fout 将打印到文本文件中,但事实并非如此。谁能指出我正确的方向?我是这个社区的新手,所以我希望我的问题有意义/代码清晰。谢谢!
【问题讨论】:
标签: c++