Struct padding in C++

后端 未结 4 931
误落风尘
误落风尘 2020-11-22 09:11

If I have a struct in C++, is there no way to safely read/write it to a file that is cross-platform/compiler compatible?

Because if I understand correct

4条回答
  •  逝去的感伤
    2020-11-22 10:08

    No, there's no safe way. In addition to padding, you have to deal with different byte ordering, and different sizes of builtin types.

    You need to define a file format, and convert your struct to and from that format. Serialization libraries (e.g. boost::serialization, or google's protocolbuffers) can help with this.

提交回复
热议问题