将未签名的数据写入 C++ 中的二进制文件
writing unsigned data to binary file in C++
我正在尝试编写序列
56 4F 4C 03
作为二进制(无符号字符)放入文件中。
在 ASCII 中,这应该拼写出"VOL",根据我的十六进制编辑器,对应于字节"03"的第 4 个字符是非显示字符。
下面正确写入"VOL",但我不知道如何编写原始无符号的"03"数据,因为它是一个不显示的字符。我该怎么做?我应该使用像0x03这样的标识符进行编码,还是有更清洁的方法?
int main()
{
std::ofstream outfile;
outfile.open ("data.vol", std::ios::out | std::ios::binary | std::ios::app);
std::string str = "VOL";
std::string::size_type sz = str.size();
// when i leave this line out, it writes to string?
outfile.write(reinterpret_cast<char*>(&sz), sizeof(std::string::size_type));
outfile.write(str.data(),sz);
std::string m_version_identifier = "3";
outfile.write((char*)&m_version_identifier.data()[0],m_version_identifier.size());
outfile.close();
std::cout << "done" << std::endl;
return 0;
}
您正在寻找字符' 3'
或字符串"VOL 3"
。
写入字节本身可能是更干净的方法:
int version = 3;
outfile.write(&version,1);
相关文章:
- 正在读取二进制文件(is_open)
- 在C++中将类(带有Vector成员)保存为二进制文件
- 如何从二进制文件中读取字符串
- 保存/加载大量短数组到二进制文件
- 从二进制文件中读取整数数组
- Android 在编译二进制文件时重建静态库
- 在 C++ 中将双精度变量写入二进制文件
- clang 的 libFuzzer 可以在同一二进制文件中测试超过 1 个 API 吗?
- C++:实际上不是从二进制文件中读取
- 如何从二进制文件中的给定符号中获取调用程序图
- 将内部带有矢量的结构保存/读取到二进制文件中
- 编译多个C++文件.调用二进制文件以运行代码
- 如何使用位字段将数据从二进制文件复制到结构中?
- uint8_t同一二进制文件的不同十进制值
- C++单个生成文件多个二进制文件
- 尝试将数字写入二进制文件时引发异常
- C++中读/写二进制文件
- 如何忽略某些二进制文件的执行?
- 对在不同二进制文件中创建的对象文件的依赖关系
- 我的 SDL2 程序需要哪些二进制文件,以便它在另一台未安装 SDL2 的计算机中工作