Writing a string to the end of a file (C++)

后端 未结 4 2205
逝去的感伤
逝去的感伤 2021-02-12 16:06

I have a program already formed that has a string that I want to stream to the end of an existing text file. All of what little I have is this: (C++)

 void main(         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-12 16:40

    To append contents to the end of files, simply open a file with ofstream (which stands for out file stream) in app mode (which stands for append).

    #include 
    using namespace std;
    
    int main() {
        ofstream fileOUT("filename.txt", ios::app); // open filename.txt in append mode
    
        fileOUT << "some stuff" << endl; // append "some stuff" to the end of the file
    
        fileOUT.close(); // close the file
        return 0;
    }
    

提交回复
热议问题