How to write std::string to file?

前端 未结 3 1998
半阙折子戏
半阙折子戏 2020-11-28 06:19

I want to write a std::string variable I am accepting from the user to a file. I tried using the write() method and it writes to the file. But when

3条回答
  •  情话喂你
    2020-11-28 06:32

    Assuming you're using a std::ofstream to write to file, the following snippet will write a std::string to file in human readable form:

    std::ofstream file("filename");
    std::string my_string = "Hello text in file\n";
    file << my_string;
    

提交回复
热议问题