开发者

writing binary data (std::string) to an std::ofstream?

开发者 https://www.devze.com 2023-02-18 12:43 出处:网络
I have an std::string object containing binary data which I need to write to a file. Can ofstream f(\"name\"); f << s; be problematic in any way? I need to read the data back exactly as it was o

I have an std::string object containing binary data which I need to write to a file. Can ofstream f("name"); f << s; be problematic in any way? 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 to ei开发者_StackOverflowther method?


You should be ok as long as you open the ofstream for binary access.

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

Don't forget to open your file in binary mode when reading the data back in as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消