I am having following issue with reading binary file in C.
I have read the first 8 bytes of a binary file. Now I need to start reading from the 9th byte. Following is the code:
fseek(inputFile, 2*sizeof(int), SEEK_SET);
However, when I print the contents of the array where I store the retrieved values, it still shows me the first 8 bytes which is not what I need.
Can anyone please help me out with this?
fseekjust moves the position pointer of the file stream; once you’ve moved the position pointer, you need to callfreadto actually read bytes from the file.However, if you’ve already read the first eight bytes from the file using
fread, the position pointer is left pointing to the ninth byte (assuming no errors happen and the file is at least nine bytes long, of course). When you callfread, it advances the position pointer by the number of bytes that are read. You don’t have to callfseekto move it.