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

后端 未结 4 2206
逝去的感伤
逝去的感伤 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条回答
  •  无人共我
    2021-02-12 16:40

    I hope that isn't your whole code because if it is, there's lots of things wrong with it.

    The way you would write out to a file looks something like this:

    #include 
    #include 
    
    // main is never void
    int main()
    {
        std::string message = "Hello world!";
    
        // std::ios::out gives us an output filestream
        // and std::ios::app appends to the file.
        std::fstream file("myfile.txt", std::ios::out | std::ios::app);
        file << message << std::endl;
        file.close();
    
        return 0;
    }
    

提交回复
热议问题