Windows 到 iPhone 二进制文件

发布于 2024-12-05 05:05:42 字数 434 浏览 0 评论 0原文

将二进制文件从 Windows 传递到 iPhone 是否安全,这些文件的编写方式如下:

std::ostream stream = // get it somehow
stream.write(&MyHugePODStruct, sizeof(MyHugePODStruct));

读取方式如下:

std::istream stream = // get it somehow
stream.read(&MyHugePODStruct, sizeof(MyHugePODStruct));

虽然 MyHugePODStruct 的定义相同?如果没有,有什么方法可以使用标准库(包括 c++11)或安全地升压来序列化它?有没有更干净的方法,因为它看起来像是一段不可移植的代码?

Is it safe to pass binary files from Windows to iPhone that are written like:

std::ostream stream = // get it somehow
stream.write(&MyHugePODStruct, sizeof(MyHugePODStruct));

and read like:

std::istream stream = // get it somehow
stream.read(&MyHugePODStruct, sizeof(MyHugePODStruct));

While the definition of MyHugePODStruct is the same? if not is there any way to serialize this with either standard library (c++11 included) or boost safely? is there more clean way to this, because it seems like a non portable piece of code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚笙 2024-12-12 05:05:42

不,有很多原因。首先,这不会编译,因为您必须将 char * 传递给 readwrite。其次,这甚至不能保证在一个平台上工作,因为该结构可能包含填充(但即使在同一平台上,不同编译版本的代码之间的填充本身也可能有所不同)。接下来,需要考虑影响许多基元类型的 64/32 位问题(例如,long double 在 x86 上填充为 12 字节,但在 x64 上填充为 16 字节)。最后但并非最不重要的一点是字节序(尽管我不确定 iOS 字节序是什么)。

简而言之,不,不要这样做。

您必须根据其数据类型单独序列化每个结构成员。

您可能想查看 Boost.serialization,尽管我没有这方面的经验。

No, for many reasons. First off, this won't compile, because you have to pass a char * to read and write. Secondly, this isn't guaranteed to work on even one single platform, because the structure may contain padding (but that itself may differ between different among differently compiled versions of the code, even on the same platform). Next, there are 64/32-bit issues to consider which affect many of the primitive types (e.g. long double is padded to 12 bytes on x86, but to 16 bytes on x64). Last but not least there's endianness (though I'm not sure what the iOS endianness is).

So in short, no, don't do that.

You have to serialize each struct member separately, and according to its data type.

You might like to check out Boost.serialization, though I have no experience with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文