write and read string to binary file C++
To write a std::string to a binary file, you need to save the string length first: std::string str(“whatever”); size_t size=str.size(); outfile.write(&size,sizeof(size)); outfile.write(&str[0],size); To read it in, reverse the process, resizing the string first so you will have enough space: std::string str; size_t size; infile.read(&size, sizeof(size)); str.resize(size); infile.read(&str[0], size); Because strings have a variable size, unless … Read more