所以我希望能够在控制台关闭后打开文件,并能够继续添加到文件中。
int main(){
string inputWord;
ofstream theFile("Info.txt");
cout << "Type Word or Sentence: ";
while(getline(cin,inputWord)){
cout << "Type Word or Sentence: ";
theFile << "Word or Sentence: " << inputWord;
theFile << "(Regular Value): " << ch2n(inputWord) << endl;
theFile << "(Other Value): " << char2num(inputWord) << endl;
theFile << "(Sum): " << ch2n(inputWord) + char2num(inputWord) << endl;
theFile << "(Difference): " << ch2n(inputWord) - char2num(inputWord) << endl;
theFile << "(Total): " << ch2n(inputWord) + (ch2n(inputWord)+char2num(inputWord)) + (ch2n(inputWord)-char2num(inputWord)) + char2num(inputWord) << endl << endl;
if(inputWord == "")return 0;
}
}
答案 0 :(得分:4)
您必须以追加模式打开文件流:
std::ofstream out("Info.txt", std::ios_base::app);
// ^^^^^^^^^^^^^^^^^^
答案 1 :(得分:0)
http://www.cplusplus.com/reference/fstream/fstream/open/
在这里也有一个例子。
// fstream::open / fstream::close
#include <fstream> // std::fstream
int main () {
std::fstream fs;
fs.open ("test.txt", std::fstream::in | std::fstream::out | std::fstream::app);
fs << " more lorem ipsum";
fs.close();
return 0;
}