读取保存为结构块的二进制文件,并将其读入正文数组 [英] Read the binary file saved as a structure block and read it into the body array

查看:82
本文介绍了读取保存为结构块的二进制文件,并将其读入正文数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct TDBDefine_Tick * data = new TDBDefine_Tick [N]; //新结构数组会出错

TDBDefine_Tick:这是保存在二进制文件中的结构块



struct TDBDefine_Tick * data =(TDBDefine_Tick *)malloc(sizeof(struct TDBDefine_Tick)* N);

当使用另一个新的内存空间时,以下读取将出错



我尝试过:



struct TDBDefine_Tick *data=new TDBDefine_Tick[N]; //new structure array will go wrong
TDBDefine_Tick:This is the structure block that is saved in the binary file

struct TDBDefine_Tick *data=(TDBDefine_Tick *)malloc(sizeof(struct TDBDefine_Tick )*N);
When using another new memory space, the following read will go wrong

What I have tried:

int ReadFile()
{
	 
	FILE *fp;
	if ((fp = fopen("C:\ding.bs", " rb")) == NULL)
	{
		return -1;
	}
	else
	{
		fseek(fp,0,SEEK_END);
		long size=ftell(fp);
		long N=size/sizeof(TDBDefine_Tick);
		struct TDBDefine_Tick *data=new  TDBDefine_Tick[N];
	
	for (int i=0; i<N; i++)
	{
		fseek(fp, i * sizeof(struct TDBDefine_Tick), 0); 
		fread(&data[i], sizeof(struct TDBDefine_Tick), 1, fp);
		 
		 
	}
	}
return 0;
}

推荐答案

Quote:

当使用另一个新的存储空间时,以下读取将出错

When using another new memory space, the following read will go wrong

你应该详细说明。



请注意你不需要 fseek 迭代地,由于不正确的读取,文件指针已经存在。此外,您可以在单个读取操作中读取所有数组项。

You should detail that.

Please note you don't neeed to fseek iteratively, the file pointer is already there, due to previaous read. Moreover you could read all the array items in a single read operation.


这篇关于读取保存为结构块的二进制文件,并将其读入正文数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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