Question: Using fread() to read binary data one record at a time. My assignment requires reading a binary file using fread() . It says The fields
Using fread() to read binary data one record at a time. My assignment requires reading a binary file using fread() .
It says The fields of the records are stored using the types specified in the data file description. The fields are stored packed next to each other in the data file. You cannot read the entire record directly into a C struct in one call because C inserts additional unused space between some of the fields in the struct (this is called alignment padding.
You must read the data record one field at a time. It is suggested to use fread to read each field.
My code reads a struct record of various data types. filename is f1.
I have tried 2 methods
First method:
fread(&rec1,sizeof(struct record),1,f1);
Second method:
fread(&rec1.tin ,sizeof(struct record),1,f1); printf("%hhu, ",rec1.tin); fread(%rec1.love,sizeof(struct record),1,f1); printf("%s, ",rec1.love); fread(&rec1.curve,sizeof(struct record),1,f1); printf("%d, ",rec1.curve); fread(&rec1.history,sizeof(struct record),1,f1); printf("%lu, ",rec1.history); fread(&rec1.ground,sizeof(struct record),1,f1); printf("%u, ",rec1.ground); fread(&rec1.week,sizeof(struct record),1,f1); ... and so on None of the above work for me as test file returns
**** Automarker: fread must read each struct field separately
**** Output ignored. Your program exited with error code: 202
HELP appreciated ! :)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
