我需要将变量的值写入持久文件。每个写入操作都应该在同一个文件中添加一个新行,并且该文件最终将包含来自数百万个写入操作的数百万行数据。我在下面写的代码确实写入了文件,但问题是它一次又一次地覆盖了同一行。另外,我不确定我是否正确地转义了行尾,因为它还没有输出多行。任何人都可以告诉我如何修改下面的代码,使其写入尽可能多的数据行吗?
#include <sstream>
#include <string>
void writeToFile(std::stringstream& ss){
using namespace std;
string myString = ss.str();
ofstream myfile;
myfile.open ("somefile.txt");
myfile << myString;
myfile.close();
}
void anotherFunction(){
using namespace std;
stringstream mySS;
mySS << someVar; mySS << ", "; mySS << var2;
mySS << ", "; mySS << something; mySS << ", "; mySS << somethingelse; mySS << ", ";
mySS << dayofweek; mySS << ", "; mySS << time; mySS << ", ";
mySS << whichbone; mySS << ", "; mySS << name; mySS << "\n" << endl;
writeToFile(mySS);
}
void aThirdFunction(){
using namespace std;
stringstream mySS;
mySS << someVar; mySS << ", "; mySS << var2;
mySS << ", "; mySS << var3; mySS << ", "; mySS << fourthVar; mySS << ", ";
mySS << fifthVar; mySS << ", "; mySS << sixthVar; mySS << "\n" << endl;
writeToFile(mySS);
}