Monday, December 12, 2011

Please help me.i want to open one binary file.?

i want to open one binary file and extract data of it(data are in 12 bit format i.e first 8 bit and 4 bit of the next 8 bit,must save in one variable indeed 3 of this 8 bit need to extract 2 different data) but i don't know why below code execute and extract data(8 bit by 8 bit) only 16383 time(in


maximum case) but i want to extract all data in this file that is about 650000.what is problem?i think it is in variable define.is it right?


note:"signalh" is the path that user enter.and "se_2" is the number that execute by this equation:"se_2=360*se_1" and se_1 is the float number that user enter and is beetwen "0 to 1800" so we will have maximum value i.e 650000 for se_2 if user enter 1800 for se_1(the whole file)


it is part of code:


/* define variables*/


const int c1=15,c2=8,c3=128;


int m1h,m2h,prl,prr;


unsigned int i1=1;


float *sig1,*sig2;


int p1=0,p2=0,p3=0;


/*opening file*/


f2=fopen(signald,"rb");


if (f2==NULL){


printf("error in opening dat file");


getch();


exit(1);


}


sig1=(float *)malloc(sizeof(float)*((se_2)));


sig2=(float *)malloc(sizeof(float)*((se_2)));


if ((sig1!=NULL)%26amp;%26amp;(sig2!=NULL)){


printf("\n successfull allocation.");


getch();


}


else{


exit(1);


}


for (i1=1;i1%26lt;=se_2;i1++){


fread(%26amp;p1,1,1,f2);


fread(%26amp;p2,1,1,f2);


fread(%26amp;p3,1,1,f2);


m2h=((p2%26gt;%26gt;4)%26lt;%26lt;c2);/*to separate 4 bit of second 8 bit*/


m1h=((p2%26amp;c1)%26lt;%26lt;c2);


prl=((p2%26amp;c2)%26lt;%26lt;9);


prr=((p2%26amp;c3)%26lt;%26lt;5);


sig1[i1]=((m1h)+(p1)-(prl));/*execute first data from first 8 bit and the next 4 bit*/


sig2[i1]=((m2h)+(p3)-(prr));/*execute second data


p1=p2=p3=m1h=m2h=prl=prr=0;


}


fclose(f2);


getch();


free(sig1);


free(sig2);|||So, the problem is that the loop will only execute to a maxium of 16384 times?


Then the problem is with the "se_2" variable. Your FOR loop repeats "se_2" number of times. Also, remember that your FOR loop reads THREE bytes at a time. So, if your data file is 650000 bytes in size, then your FOR loop must only execute 650000/3 times.


Check the data type of "se_2" to see if it is the correct one and you may need to alter your FOR loop line or fix the value of "se_2" so the FOR loop executes the proper number of times.





One last thing that you should do is that you should put in error checking code for fread(). You did this for fopen(), but that is not good enough. I suggest that you put error checking code after every fread() in your loop. It is possible for the user to input the wrong number for se_1 (in relation to the size of the data file), and then your FOR loop will run an incorrect number of times.


After any type of code for getting user input or any type of file I/O, you should ALWAYS have error checking code.

No comments:

Post a Comment