Hi all,
Sorry to be reporting to an old thread. But I just realised that the problem that I announced as having been fixed above was actually not properly fixed. The issue was that I was unable to fseek beyond a certain number of rows in my file because the offset number was overflowing the max number that could be held in an int.
To fix this, i tried to do a stepped fseek as given in the above code. Basically, if my offset value was beyond the max possible int, I fseeked until the max possible row. And then fseek again to move over the remaining rows.. I thought that this had fixed the problem because I started to get some output. But i neglected to verify that output at that time. I now realised that though the filepointer moves to somepoint in the file.. Its not to the correct point.
I ve tried googling about but didnt turn up anything. Does anyone know if doing 2 fseeks one after the other is known to have any issues.
To test the code, I created a huge binary file in which each row is an array of floats filled with the row number. So by printing out the array which is read, I can know what is the array I am reading. This code is appended below
Code:
#include <stdio.h>
int main()
{
FILE *fp;
float *array;
int i, j;
int n_records = 181500, mz_range = 2960; /** 181375 is the no of rows at which my offset over flows the max int value. So just picked an arbitrary value above that to be the no of rows **/
if( (fp =fopen("fseek_test.txt","wb")) == NULL)
{
printf("Error.. can't open fseek_test.txt\n");
exit(0);
}
array = (float*) malloc(mz_range*sizeof(float));
fprintf(fp,"File created for the purpose of testing my stepped fseek code\n");
for(i=0; i<n_records; i++)
{
for( j=0; j<mz_range; j++)
{
array[j] = i;
}
fwrite(array, sizeof(float), mz_range, fp);
}
fclose(fp);
printf("Test file created!!\n");
return 0;
}
The code to test if my stepped fseek works is given below.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp;
char szbuf[5000];
int i, max_fseekrow, mz_range = 2960;
int row = 181500, offset_row;
float *array;
if( (fp=fopen("fseek_test.txt","rb")) ==NULL)
{
printf("Error.. unable to open fseek_test.txt\n");
exit(0);
}
array = (float*) malloc(mz_range*sizeof(float));
fgets(szbuf,5000,fp); /** reading out the title line **/
printf("%s\n\n",szbuf);
max_fseekrow = (int) 2147483647 /( mz_range * sizeof(float) ); /** Computing max_fseekrow.. 2147483647 is the biggest possible 32 bit integer **/
offset_row = row;
printf("Offset row= %d\n",offset_row);
fseek_checkpt:
if( offset_row > max_fseekrow)
{
fseek(fp, max_fseekrow*mz_range*sizeof(float), SEEK_CUR);
printf("Done with the first fp movement\n");
if( (fread(array, sizeof(float), mz_range, fp)) != mz_range)
{
printf("Possible read error in fread");
exit(0);
}
for( i=0; i<10; i++)
{
printf("%f\t",array[i]);
}
offset_row = offset_row - max_fseekrow -1;
goto fseek_checkpt;
}
else
{
if( ( fseek(fp, offset_row*mz_range*sizeof(float), SEEK_CUR)) != 0)
{
printf("Error.. fseek didnt work!\n");
exit(0);
}
}
if( (fread(array, sizeof(float), mz_range, fp)) != mz_range)
{
printf("Possible read error in fread");
exit(0);
}
for( i=0; i<10; i++)
{
printf("%f\t",array[i]);
}
fclose(fp);
return 0;
}
As you can see, what I try in the code is just to calculate the max number of rows that I can fseek over without the offset value overflowing int. Then if the offset rows exceeds this max number of rows, then I first fseek over the max_fseekrows. And then to get to the point that i wanted to go to, I fseek over the rest of the file (offset_row - max_fseekrow). [Note: since I read in 1 row to check if the first fseek worked properly, I am setting offset_row to offset_row - max_fseekrow -1.]
The fread after the first fseek gives out the proper line. But the fread after the second fseek only prints out zeros. Irrespective of whether I want to offset it by offset it by offset_rows or just 1 row. It all gives out a zero value. Commenting out the second fseek and just doing freads hawever gives me proper output. So I am sure the file isn;t empty or anything. Does anyone have any ideas on what I am doing wrong.. I would be grateful for any advice..
best,
Avinash