Reading binary files c -


so basicaly have binary file made such structure

struct data{ char name[30]; char name2[30]; }; 

i want read data array of structs file problem dont know how many records there in file. explain how read whole file not given ammout of records inside?

you can open file, check it's size:

fseek(fp, 0l, seek_end); // go @ end sz = ftell(fp);          // tell me current position fseek(fp, 0l, seek_set); // go @ beginning 

and number of records inside be:

n = sz/sizeof(struct data); 

anyway, careful if write array of structures file, it's possible not readable others machines, due different memory alignment. can use __attribute__((packed)) option sure structure same (but it's gcc specific extension, not part of standard c).

struct __attribute__((packed)) data {     char name[30];     char name2[30]; }; 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -