开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜