Pascal 类型化二进制文件的 C++ 和 Python 类似物

C++ and Python analogs for Pascal's typed binary files

Pascal 中有一个方便的东西,就是类型化的二进制文件。

描述来自Free Pascal wiki

The type file of <type>, where <type> is any simple type (no references/pointers) or record type, is a binary file representing a sequence of values. For example, you can have files that are a sequence of integers, floating-point values, or records (as long as all record fields are also simple types).

我想知道我是否可以将类似的东西用于 Python 数据类型、C++ 类 或类似 C 的普通结构,即比原始数字类型更复杂的东西。

在 C++ 中,文件布局由程序员决定。
该语言提供了一种 binary 模式,在这种模式下,数据不会与文件相互转换。

此外,某些类型可能包含 指针 ,它们在写入文件时有效,因为指向的项目可能会被操作系统加载到不同的位置。

在互联网上搜索 "c++ serialization"。

Python(3)基本上有三种类型的文件;

  • 文字
  • 二进制
  • 原始

请参阅 io 模块的文档。

使用二进制文件和 struct 您基本上可以在任意文件类型上创建迭代器。