I am having trouble printing the dates from a binary file. Here is the table that I am using for reference:
Here is my code:
Code:
#include <stdio.h>
struct MiscBits
{
unsigned id: 2;
unsigned category: 2;
unsigned engaged : 1;
unsigned reserved: 3;
};
struct Date {
unsigned year: 7;
unsigned month: 4;
unsigned day: 5;
unsigned hour: 5;
unsigned minute: 6;
unsigned reserved2: 5;
unsigned second: 6;
unsigned reserved3: 10;
};
struct Record {
float latitude;
float longitude;
short altitude;
char name[5];
struct MiscBits Misc;
char Reported[6];
struct Date myDate;
} __attribute__((packed));
int main(){
FILE *file;
struct Record myRecord;
file = fopen("tracks2.dat", "rb");
for(int i=0;i<4;i++) {
fread(&myRecord,22,1,file);
printf("latitude: %f longitude: %f altitude: %d name: %s ", myRecord.latitude,myRecord.longitude,myRecord.altitude,myRecord.name);
printf("%d %d %d %d %d %d %d %d", myRecord.myDate.year, myRecord.myDate.month, myRecord.myDate.day, myRecord.myDate.hour, myRecord.myDate.minute, myRecord.myDate.second);
switch (myRecord.Misc.id) {
case 0: printf("%s ", "ID: Uknown");
break;
case 1: printf("%s ", "ID: Friend");
break;
case 2: printf("%s ", "ID: Foe");
break;
case 3: printf("%s ", "ID: Neutral");
break;
}
switch (myRecord.Misc.category) {
case 0: printf("%s ", "Category: Ship");
break;
case 1: printf("%s ", "Category: Ground vehicle");
break;
case 2: printf("%s ", "Category: Airplane");
break;
}
switch (myRecord.Misc.engaged){
case 0: printf("%s\n ", "Not Engaged");
break;
case 1: printf("%s\n ", "Engaged");
break;
}
}
fclose(file);
return(0);
}
My program prints the other structs out correctly but not the date struct.