如何打开txt文件而不删除先前的内容 [英] How Do I Open A txt File Without It Erasing The Previous Content

查看:55
本文介绍了如何打开txt文件而不删除先前的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I want to be able to open up the file after the console closes and be able to continue adding to the file.

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


推荐答案

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

You have to open the file stream in append mode:

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

这篇关于如何打开txt文件而不删除先前的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆