So I added #define _FILE_OFFSET_BITS 64 to the top of all my .h and .c files (probably overkill, but where's the harm?). Is there any way to tell if it's actually doing anything?
The code itself was modified as follows:
Code:
int64_t read_current(FILE *input, double *current, uint64_t position, uint64_t length)
{
uint64_t test;
double iv[2];
uint64_t i;
int64_t read = 0;
if (fseek(input,position*2*sizeof(double),SEEK_SET))
{
return 0;
}
for (i = 0; i < length; i++)
{
test = fread(iv, sizeof(double), 2, input);
if (test == 2)
{
read++;
swapByteOrder((uint64_t *) &iv[0]);
current[i] = iv[0];
}
else
{
perror("End of file reached: ");
break;
}
}
printf("read %" PRId64 "\n",read);
return read;
}
I have to say thanks to anduril for the suggestion to use fread instead of fscanf - the program is literally an order of magnitude faster now. Crazy.
I realized that the way things are set up I can't actually get around the use of fseek() - the chunks of data I read might overlap slightly and so I do need to reposition the file pointer on each call. I was wondering about the use of off_t vs uint64_t - is it enough to take my uint64_t and cast it to an off_t, or do I need to do something else? eg
Code:
if (fseek(input,(off_t) (position*2*sizeof(double)),SEEK_SET))
EDIT: I did a
Code:
printf("%d\n",sizeof(off_t));
which printed 4, whereas off64_t printed 8 - so I think the function test macro is not doing it's job as implemented... So for now I will stick with the 64 versions explicitly, since portability is not something I will be needing for the time being.