write and read string to binary file C++

后端 未结 7 758
暖寄归人
暖寄归人 2020-12-09 12:46

Im having problems writing string into a binary file. This is my code:

ofstream outfile(\"myfile.txt\", ofstream::binary);
std::string text = \"Text\";
outfi         


        
7条回答
  •  既然无缘
    2020-12-09 13:16

    • Why are you using pointers to std::string class?
    • You should not use sizeof with std::string, as it returns the size of the std::string object, and not the real size of the string inside.

    You should try:

    string text = "Text";
    outfile.write(text.c_str(), text.size());
    

    or

    outfile << text;
    

提交回复
热议问题