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

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -