I could use some enlightment on binary file i/o, especially in conjunction with writing libraries. I'm trying to play with the JPL ephemerides routines available at http://ephemeris.com/software.html. I can compile the code and run it with no problem. All the tests run successfully.
The problem comes in when I try to move the gnulliver.c, ephcom.c, and ephcom.h files off into a compiled library and then use them. I'm using the testeph program in this case but am seeing the problem no matter how I access the library routines.
The problem appears to arise in ephcom.c, line 970. When being run in a library the feof(infp) evaluates to true early. It varies depending on the particular call but it is dies when the loop index is somewhere in the 16 to 38 range. When this code is run when compiled directly with the test program, the loop runs through fully to i = 1018.
There are no code changes at all. The only difference is that in one I'm compiling the four files:
ephcom.c
ephcom.h
gnulliver.c
testeph.c
Together to get an executeable. That way works. The other way, I compile the first three files together into a library and then compile testeph.c and link it with that compiled library. I've tried using netbeans 6.5/gcc and MS Visual Studio Express 2008 and see the same thing. I've tried making the library both a statically linked one or a dynamically linked one.
I know that I'm not real clear on the details of passing FILE *pointers to libraries and suspect that it may be related to that. The part that perplexes me the most is that the reading from the file works for the first few values per block and then suddenly breaks down.
The function where things break down is:
Code:
int ephcom_readbinary_block(
FILE *infp, /* File pointer for direct access file */
struct ephcom_Header *header, /* header struct, already filled in */
int blocknum, /* Data block number, starting with 0 */
double *datablock /* returned coefficient data block */
) {
int i;
long filebyte;
double ephcom_indouble(FILE *);
int fseek(FILE *, long, int);
filebyte = (blocknum + 2) * header->ncoeff * 8; /* 8 bytes per coefficient */
fseek(infp, filebyte, SEEK_SET);
// printf("Blocknum: %d, Byte: %d\n", blocknum, filebyte);
for (i=0; !feof(infp) && i<header->ncoeff; i++) {
datablock[i] = ephcom_indouble(infp);
}
if (i < header->ncoeff && feof(infp)) i = -1; /* 0 --> EOF */
return(i); /* Number of coefficients successfuly read (all or nohing). */
}
Any specific help or any general pointers to info about using FILE *pointers with libraries would probably be helpful.
Thanks in advance.