简体   繁体   中英

fread - Read and skipping memory contents

If I am reading a file as:

fptr = fopen(read_path, "rb");
fread(contents, size_in_bytes, count, fptr);

Let's say I want to read 4 bytes and then skip 4 bytes and then read 4 and so on. How can I do that? Specifically, how will I manipulate/advance the fptr ?

An example using fseek :

FILE * fptr;

fptr = fopen("example.txt", "rb");
fread(contents, 1, 4, fptr);
fseek(fptr, 4, SEEK_CUR);
// ...
fclose(fptr);

我认为你正在寻找的功能是fseek

If the amount to be skipped is small, you can always read it into a dummy and dispose of it:

char contents[SOME_SIZE];
char dummy[4];

fptr = fopen(read_path, "rb");
fread(contents, 4, 1, fptr);
fread(dummy, 4, 1, fptr);
fread(contents+4, 4, count-1, fptr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM