Reading binary in C

I am having the following problem reading a binary in C.

I read the first 8 bytes of the binary. Now I need to start reading from the 9th byte. Below is the code:

fseek(inputFile, 2*sizeof(int), SEEK_SET);

However, when I print the contents of the array, where I store the received values, it still shows me the first 8 bytes that I do not need.

Can anyone help me with this?

+3
source share
2 answers

fseekjust moves the file stream position indicator; once you have moved the position indicator, you need to call freadto actually read the bytes from the file.

, fread, ( , , , ). fread, . fseek, .

+8

, :

FILE* file = fopen(FILENAME, "rb");
char buf[8];

8 , 8 :

/* Read first 8 bytes */
fread(buf, 1, 8, file); 
/* Read next 8 bytes */
fread(buf, 1, 8, file);

8 fseek 8 (8.. 15 , 0):

/* Skip first 8 bytes */
fseek(file, 8, SEEK_SET);
/* Read next 8 bytes */
fread(buf, 1, 8, file);

, C . fread , , fread . fseek .


P.S.: . ( 1 fread)

+10

Source: https://habr.com/ru/post/1739561/


All Articles