如何读取二进制文件到矢量矢量结构的矢量 [英] How to read a binary file to a vector of vector vector structure

查看:92
本文介绍了如何读取二进制文件到矢量矢量结构的矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 std :: vector< std :: vector< std :: vector< sphere_data> >> Sphere_image; 
这就是我将数据写入二进制文件
of of fout的方式( C: /Project/data.dat,ios :: out | ios :: binary);
for const auto & dim:Sphere_image)
for const auto & dim2:dim)
fout.write(reinterpret_cast< const char *>(dim2.data()),dim2 .size()* sizeof (sphere_data));

fout.close();

如何将二进制文件读入下面的向量。
std :: vector< std :: vector< std :: vector< sphere_data> >> Sphere_image_read;





我尝试过:



std :: vector< std :: vector< std :: vector< sphere_data

of of fout(C:/Project/data.dat,ios :: out | ios :: binary );

for(const auto& dim:Sphere_image)

for(const auto& dim2:dim)

fout.write( reinterpret_cast< const char *>(dim2.data()),dim2.size()* sizeof(sphere_data));



fout.close();

解决方案

正如Richard所写,你必须通过读取数据并重新构建你的对象来恢复写入过程:

 ifstream fin(  C:/Project/data.dat,ios :: in | ios :: binary); 
// 计算球体数
int count = fileSize / sizeof (sphere_data);
// 所有数据的循环
for int n = 0 ; n< count; n ++){
// 读取一个数据集
fin.read(reinterpret_cast< const char * = >(dim。 data()),dim.size()* sizeof (sphere_data));
// 添加到您的球体
}
fin.close ();< / const的>


std::vector<std::vector<std::vector<sphere_data> >> Sphere_image ;
This is how I have written the data to a binary file
ofstream fout("C:/Project/data.dat", ios::out | ios::binary);
			for (const auto &dim: Sphere_image)
			for (const auto &dim2:dim)
				  fout.write(reinterpret_cast<const char*>(dim2.data()), dim2.size() * sizeof(sphere_data));
		
fout.close();

How to read the binary file into the below vector.
std::vector<std::vector<std::vector<sphere_data> >> Sphere_image_read ;



What I have tried:

std::vector<std::vector<std::vector<sphere_data
ofstream fout("C:/Project/data.dat", ios::out | ios::binary);
for (const auto &dim: Sphere_image)
for (const auto &dim2:dim)
fout.write(reinterpret_cast<const char*>(dim2.data()), dim2.size() * sizeof(sphere_data));

fout.close();

解决方案

As Richard wrote you must revert the write process by reading the data and re-construct your object like that:

ifstream fin("C:/Project/data.dat", ios::in| ios::binary);
//calculate sphere count
int count = fileSize /  sizeof(sphere_data);
//loop for all data
for(int n = 0; n < count; n++ ) {
//read one data set
fin.read(reinterpret_cast<const char*="">(dim.data()), dim.size() * sizeof(sphere_data));
//add to your sphere
}
fin.close();</const>


这篇关于如何读取二进制文件到矢量矢量结构的矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆