Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
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;
}
Posted
Updated 11-Oct-17 21:25pm
v2
Comments
Richard MacCutchan 12-Oct-17 3:39am    
What do you mean by "the following read will go wrong"?
Please show the definition of your TDBDefine_Tick structure and explain exactly what happens when you try to read the file.

1 solution

Quote:
When using another new memory space, the following read will go wrong
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.
 
Share this answer
 
Comments
DingTingTing 12-Oct-17 3:35am    
Even if Fseek is removed, fp pointing to the file header will be wrong for the for loop。The main question is not this
CPallini 12-Oct-17 4:14am    
"Even if Fseek is removed, fp pointing to the file header will be wrong for the for loop"
What 'file header'?
You didn't tell us about a 'file header'.
The main question is not this
So, please, tell us what is the main question. Namely detail what 'will go wrong'.
[no name] 15-Oct-17 7:01am    
"Even if Fseek is removed...": But you made sure that before you start reading the you are beginning of file?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900