在C++中使用 CArchive 类从二进制文件中读取短数据
Read short data from binary file using CArchive class in C++
我创建了一个应用程序,将数组short
存储到文件中CArchive class
。
用于保存数据的代码
CFile objFile(cstr, CFile::modeCreate | CFile::modeWrite);
CArchive obj(&objFile, CArchive::store);
obj << Number; //int
obj << reso; //int
obj << height; //int
obj << width; //int
int total = height * width;
for (int i = 0; i < total; i++)
obj << buffer[i];//Short Array
这是我用来将数据保存在文件中的代码片段。
现在我想使用 CArchive
打开该文件。
我试图使用 fstream
打开它。
std::vector<char> buffer(s);
if (file.read(buffer.data(), s))
{
}
但是上面的代码并没有给我保存的相同数据。那么,anyuone 能否告诉我如何使用CArchive
或任何其他函数short
数组中获取该数据。
假设缓冲区是 short 数组,加载数据的代码可以写成:
CFile objFile(cstr, CFile::modeRead);
CArchive obj(&objFile, CArchive::load);
obj >> Number; //int
obj >> reso; //int
obj >> height; //int
obj >> width; //int
int total = height * width;
//release the old buffer if needed... e.g:
if( buffer )
delete[] buffer;
//allocate the new buffer
buffer = new SHORT [total];
for (int i = 0; i < total; i++) {
obj >> buffer[i];
}
obj.Close();
objFile.Close();
相关文章:
- 如何在 c++ 中将二进制文件读取为字符串
- 从二进制文件读取到数组:前面的任意数字
- 提升二进制文件读取错误不支持的版本
- 无法将二进制文件读取到 std::vector<std::byte> 中C++
- 提升序列化在从二进制文件读取时引发异常
- 从二进制文件 C++ 读取的字符串数组写入
- 将二进制文件读取到 std::vector<bool>
- 将fortran生成的二进制文件读取到C++中的带符号整数数组中
- 将从二进制文件读取的字符串转换为整数
- 从/到C 的二进制文件读取和写作整数
- 通过将结构信息从二进制文件读取到数组中,然后返回到文件C
- 将 int 的二进制文件读取到字符串 c++
- 将结构从二进制文件读取到链表,导致无限循环
- 将二进制文件读取到向量元素的地址中
- c++从二进制文件读取/写入类
- Python:将二进制文件读取到一个结构中,然后拆包
- 从二进制文件读取/写入十六进制字节
- 简单的 C++ 二进制文件读取
- 从二进制文件 c++ 读取 16 位整数
- 如何优化c++二进制文件读取