Writing binary data (std :: string) to std :: ofstream?

I have a std::string object containing binary data that I need to write to a file. Can ofstream f("name"); f << s; ofstream f("name"); f << s; be problematic? I need to read the data back exactly as it was originally.

I can of course use fwrite(s.c_str(), s.size(), 1, filep) , are there any pros / cons for any method?

+2
source share
1 answer

You should be fine while you open the stream for binary access.

 ofstream f("name", ios::binary | ios::out); f << s; 

Remember to also open the file in binary mode while reading data.

+6
source

All Articles