读取保存为结构块的二进制文件,并将其读入正文数组 [英] Read the binary file saved as a structure block and read it into the body array
问题描述
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;
}
推荐答案
当使用另一个新的存储空间时,以下读取将出错
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屋!