I have a binary file which has following hex values.
Reading this binary file with proper code should show the following:
highlighted area in green colour from the 1st image represents fields of father, room, attraction, cake which have output values of 1,1,0,1 separately.
I have to figure out what data types are used to print out 1,1,0,1 for the struct variables in my code written below.
I could guess that 1101 comes from hex 'd'. But, I am not sure how it is printed out in 4 different fields.
I know that my question is related to structure padding or bit fields. But, I am still not sure how my code and fread should be amended.
It would be much appreciated if you can help me out with this or provide me with examples or relevant readings.
#define MAX_HAT 9
#include<stdio.h>
#include<stdlib.h>
//struct variables
struct test
{
short int land;
float experience;
char boys;
short int angle;
double industry;
int thread;
long int shoe;
float kitty;
unsigned char price;
//not sure whether this is done correctly
unsigned int father: 1;
unsigned int room: 1;
unsigned int attraction: 1;
unsigned int cake: 1;
int foot;
char hat[MAX_HAT];
char nest;
float bean;
};
int main(int argc, char **argv)
{
struct test t1;
FILE *fp;
//Input Checking Error
if (argc < 2) {
fprintf(stderr, "Usage: %s input_file\n", argv[0]);
exit(1);
}
//binary file to open for reading
fp = fopen(argv[1], "rb");
//File Checking Error
if (fp == NULL){
fprintf(stderr, "Cannot open the file %s\n", argv[1]);
exit(1);
}
//Print out struct fields
printf("land, experience, boys, angle, industry, thread, shoe, kitty, price, father, room, attraction, cake, foot, hat, nest, bean \n");
//Allocate the values into the struct
while(fread(&t1.land, sizeof(t1.land), 1, fp) == 1)
{
fread(&t1.experience, sizeof(t1.experience), 1, fp);
fread(&t1.boys, sizeof(t1.boys), 1, fp);
fread(&t1.angle, sizeof(t1.angle), 1, fp);
fread(&t1.industry, sizeof(t1.industry), 1, fp);
fread(&t1.thread, sizeof(t1.thread), 1, fp);
fread(&t1.shoe, sizeof(t1.shoe), 1, fp);
fread(&t1.kitty, sizeof(t1.kitty), 1, fp);
fread(&t1.price, sizeof(t1.price), 1, fp);
fread(&t1.father, sizeof(t1.father), 1, fp);
fread(&t1.room, sizeof(t1.room), 1, fp);
fread(&t1.attraction, sizeof(t1.attraction), 1, fp);
fread(&t1.cake, sizeof(t1.cake), 1, fp);
fread(&t1.foot, sizeof(t1.foot), 1, fp);
fread(&t1.hat, sizeof(t1.hat), 1, fp);
fread(&t1.nest, sizeof(t1.nest), 1, fp);
fread(&t1.bean, sizeof(t1.bean), 1, fp);
//Print out the outputs
printf("%d, %f, %i, %i, %f, %d, %ld, %f, %u, %, %, %, %, %x, %s, %c, %f\n", t1.land, t1.experience, t1.boys, t1.angle, t1.industry, t1.thread, t1.shoe, t1.kitty, t1.price, t1.father, t1.room, t1.attraction, t1.cake,t1.foot, t1.hat, t1.nest, t1.bean);
}
//close the file
fclose(fp);
return 0;
}

