gpt4 book ai didi

c++ - 如何打开一个txt文件而不删除之前的内容

转载 作者:搜寻专家 更新时间:2023-10-31 01:06:14 26 4
gpt4 key购买 nike

所以我希望能够在控制台关闭后打开文件并能够继续添加到文件中。

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;
}
}

最佳答案

您必须以追加模式打开文件流:

std::ofstream out("Info.txt", std::ios_base::app);
// ^^^^^^^^^^^^^^^^^^

关于c++ - 如何打开一个txt文件而不删除之前的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21176987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com