Using fread/fwrite for STL string. Is it correct?

别等时光非礼了梦想. 提交于 2019-12-05 14:54:46

You are justified in doubting this. You should only stream POD types with fwrite and fread and string is not POD.

You shouldn't do it like this, because different implementations use different structures of std::string.

In general you should only serialize integral types, the boolean type, binary data (if you can call it serializing). Make sure to use one endian-ness if you are thinking of sharing serialized data between platforms.

Watch out with floats, doubles and pointers. They can become very pesky.

You'll have to watch out with C/C++ structs too ebcause they can contain unpredictable amounts of padding.

Grzegorz Wierzowiecki

You should serialize data.

You might like to do this manually - when it comes about std::string , check out:

When it comes about more complex objects, you might be interested in things like Google Protocol Buffers and/or Thrift.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!