如何将枚举类型数据保存到文件
How to save enum-type data to file?
我有枚举类型的数据,例如:
typedef enum
{
NULLTYPE = 0U,
ParameterGetType = 1U,
ParameterSetType = 2U,
ParameterStatusType = 3U,
EventActionType = 4U,
CommandType = 5U,
CommandGetType = 6U,
CommandSetType = 7U,
EquipmentIDGetType = 15U,
EquipmentIDSetType = 16U,
EquipmentIDStatusType = 17U,
EventReportGetType = 18U,
EventReportSetType = 19U,
EventReportStatusType = 20U,
PeriodicReportType = 21U,
PeriodicReportGetType = 22U,
PeriodicReportSetType = 23U,
PeriodicReportStatusType = 24U,
CommandResponseType = 25U,
CommandResponseGetType = 26U,
CommandResponseSetType = 27U,
CommandResponseStatusType = 28U,
CommandResponseDeletedType = 29U
}MessageType;
我想使用 QByteArray 和 QFile::write(QByteArray) 将其保存到二进制文件中,
但我不知道数据的长度,以及数据的字节"分布"值,
(如果数据为 1,其他字节的值会为零吗?
我认为它不仅限于qt,如何将数据写入文件?
对于短类型数据(MsgItem->PIN),我将这样做:)
QByteArray bytes;
bytes.append(((MsgItem->PIN)>>8) & 0xff);
bytes.append((MsgItem->PIN) & 0xff);
MsgFile->write(bytes);
MsgFile->flush();
将枚举值转换为固定大小的整数(使用 <cstdint>
和例如 uint16_t
),并执行所需的任何字节顺序操作。
相关文章:
- C++将文本文件中的数据读取到结构数组中
- 将从格式化文本文件读取的文本数据存储到链表
- 如何使用位字段将数据从二进制文件复制到结构中?
- 如何将 json 数据附加到 Qt 中的现有 json 文件中
- C++ 尝试优化每一帧将数据打印到二进制文件
- 将文本和二进制数据连接到一个文件中
- 将数据从文本文件读取到C++结构中
- 将 CPP 数据转储到文件中,以便以后重复使用
- 比较 JSON::Value 变量中的数据,然后更新到文件
- C++ - 将文本追加到仅具有 NTFS"追加数据"权限的文件
- 多线程和共享资源:使用C++定期将数据从缓冲区(数据结构)复制到文件
- 如何将来自文本文件的数据读取到结构的int阵列中
- 访问向量并将数据导出到文件
- 将由结构构建的数组传递到将数据存储到文件中的函数
- 如何将枚举类型数据保存到文件
- 在c++中,将数据插入到文件中的特定位置而不进行覆盖
- 如何确保数据刷新到文件 IO (WriteFile()) 上的 HDD(未缓冲)
- c语言 - fprintf() 函数无法将数据保存到文件中;后面必须跟着 fflush() 函数吗?
- 如何在 c++ 中将字节类型的字符数组数据保存到文件中
- 可视化地将数据保存到文件c++中