如何在文件中保存/读取数据数组 (C++)
How to save/read arrays of data in a file (C++)
>我正在使用从图像计算SIFT描述符的代码。我必须处理视频中的每一帧,并存储每个计算描述符的序列。对于每个图像,描述符由两个数组组成:
Frames = (double*)realloc(Frames, 4 * sizeof(double)* nframes);
Descr = (vl_uint8*)realloc(Descr, 128 * sizeof(vl_uint8)* nframes);
如何将这两个数组的序列保存在一个文件中,然后从文件中恢复这些数据(对于视频的每一帧)?
您可以使用 ofstream 写入文件。下面的代码,可能不能直接工作,应该指向正确的方向。
#include <fstream>
#include <iterator>
#include <algorithm>
void writeDescriptors() {
std::ofstream output( "C:\descriptors.txt", std::ios::binary );
for ( int i = 1 ; i < Frames.size(); i++ )
{
out << ", " << Frames[i];
}
}
为了重新读取描述符,只需使用 ifstream 并反转算法
尝试寻找 fwrite 和 fread。
如果您有可变数量的帧 (nframe),则使用文件中的前 2*4 字节来存储文件包含的确切帧数可能会有所帮助。
编辑:http://www.cplusplus.com/reference/cstdio/fwrite/
相关文章:
- C++:__aligned(__alignof__) 导致字符数组数据出现问题?
- 很好的语法来获取对向量/数组数据的大小引用?
- 数组数据以错误的方式遍历 Python/Matlab
- 访问 Arduino 中的结构数组数据
- 在能够从 Web 浏览器访问大型数组数据的同时存储它的最可行方法是什么?
- 在C 类中声明数组数据
- 如何在 c++ 中将字符串数组数据写入输出文件
- 使用 stl::map 和 stl::unordered_map 对包含大量重复元素的数组数据进行排序
- 检索字符数组数据时出现问题
- 共享库的C 访问数组数据
- 将包含数组数据成员的结构保存到文件
- C++中的LinkedList字符数组数据
- 正在尝试使用if语句检查数组数据
- 在单独的数据中显示数组数据
- 如何在 c++ 中将字节类型的字符数组数据保存到文件中
- 从数组数据中缺少第一个元素的 DLL 返回C++结构
- 如何在C++中表示二进制字节数组数据
- 实现动态数组数据结构时出现"double free or corruption"错误
- 多维数组数据成员的运行时错误
- C++ :使用 * 显示数组数据的条形图