本文介绍了写入二进制数据(标准::字符串)到的std :: ofstream的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的std ::字符串包含我需要编写一个文件的二进制数据对象。可以通过的ofstream F(名称); F<< S; 以任何方式有问题?我需要读取的数据完全一样,它最初。

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.

我当然可以用 FWRITE(s.c_str(),s.size(),1,filep),还有什么优点/缺点,以这两种方法?

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

推荐答案

您应该没问题,只要你打开二进制文件访问的ofstream。

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.

这篇关于写入二进制数据(标准::字符串)到的std :: ofstream的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 10:37