类和结构的内部存储

发布于 2024-12-28 19:34:10 字数 859 浏览 0 评论 0原文

我想使用指针 magikry 来保存 C++ 类,使用以下将字节数据写入文件的方法:

result Osp::Io::File::Write (const void *buffer, int length);
  • 参数:

    • buffer — 指向用户提供的缓冲区的指针,其中包含要写入的字节数据
    • length — 缓冲区长度(以字节为单位)
  • 例外:< /p>

    • E_SUCCESS — 该方法成功。
    • E_INVALID_STATE — 文件尚未打开。
    • E_ILLEGAL_ACCESS — 文件未打开进行写入操作,或由于权限不足而拒绝访问。
    • E_INVALID_ARG — 发生了以下任一情况:
      • 指定的缓冲区包含空指针。
      • 指定的缓冲区长度等于或小于0。
      • 文件句柄无效(文件被其他方法关闭,或者内存已损坏)。
    • E_STORAGE_FULL — 磁盘空间已满。
    • E_IO — 由于介质突然弹出或检测到文件损坏,发生了意外的设备故障。

我宁愿不假设会有任何类型的缓冲,尽管我相信每个字节不会导致整个闪存块被重写,但我想知道是否有一种更好的方法来写入所有数据字段例如,通过指向对象的指针 (*this) 来创建类(没有其他内容,例如静态字段)?

I want to use pointer magikry to save a C++ class using the following method that writes byte data into a file:

result Osp::Io::File::Write (const void *buffer, int length);
  • Parameters:

    • buffer — A pointer to the user-supplied buffer that contains byte data to be written
    • length — The buffer length in bytes
  • Exceptions:

    • E_SUCCESS — The method is successful.
    • E_INVALID_STATE — The file has not been opened as yet.
    • E_ILLEGAL_ACCESS — The file is not opened for write operation, or access is denied due to insufficient permission.
    • E_INVALID_ARG — Either of the following conditions has occurred:
      • The specified buffer contains a null pointer.
      • The specified buffer length is equal or smaller than 0.
      • The file handle is invalid (either the file is closed by another method, or the memory is corrupted).
    • E_STORAGE_FULL — The disk space is full.
    • E_IO — An unexpected device failure has occurred as the media ejected suddenly or file corruption is detected.

I'd rather not assume that there will be any sort of buffering, although I am confident each byte won't occasion a whole block of flash to be rewritten but I was wondering if there is a niftier way to write all the data fields of a class (and nothing else, eg static fields) by, eg, a pointer to the object (*this)?

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

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

发布评论

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

评论(1

愿得七秒忆 2025-01-04 19:34:10

在 C++ 中,您不会将“原始”对象写入文件,而是将它们序列化。这并不神奇,您需要自己编写序列化代码(为了方便起见,重载运算符 <<>>)。

您可以通过仅转储内存来实现旧的 C 风格,但除了 C 语言通常会导致的问题(对齐、在系统之间传输数据时的字节序问题)之外,您还会遇到 C++ 引入的问题(内部类表示) ,可能的“隐藏”数据成员,例如v表等)。

如果您想确保读取和写入可以在不同系统和/或不同软件之间传输的可靠数据 - 您最好实现序列化,而不是寻找捷径。

您可以使用 Boost.Serialization 之类的库。

In C++ you don't write "raw" objects into files, but rather serialize them. There's no magic, you need to write your serialization code yourself (overloading operators << and >>, for convenience).

You can do it the old C-style by just dumping memory, but in addition to the problems this would generally cause with C (alignment, endian issues when transferring data between systems), you also get the problems introduced by C++ (internal class representation, possible "hidden" data members such as a v-table, etc).

If you want to ensure you read and write reliable data that can be transferred between different systems and/or different pieces of software - you better implement the serialization, and don't look for shortcuts.

You can use libraries like Boost.Serialization for that.

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