Question
I have a binary file where there are 4 records and each is 22 bytes long. My first assignment was to read that same binary
I have a binary file where there are 4 records and each is 22 bytes long. My first assignment was to read that same binary file where each record was 16 bytes long(information has been added to the file). I have the code that read the first 16 bytes of the first file and the code for that is down below. That same file has been increased. The fields that are part of "Misc" and "Date reported" have to be represented as C struct bit fields.
Misc field is one byte in length:
The Date reported field is 6 bytes in length, and divided into 3 16 bit words:
Here is my code that read the first 16 bytes(uses shifts and masks):
#include
struct record { float latitude; float longitude; short altitude; char name[5]; char misc; };
int main(){
FILE *file; struct record myRecord; file = fopen("tracks.dat", "rb"); int mask = 0b00000011;
if(file==NULL){ printf("Unable to open file"); return 1; } //array to hold extra info such as ID etc int extraInfo[3]; //loop that runs for size of file for (int i = 0; i
else if(i%16==5){ printf("latitude:%f longitude: %f altitude: %d name: %s ", myRecord.latitude, myRecord.longitude, myRecord.altitude, myRecord.name); fread(&myRecord.misc,1,1,file); for (int a=0;a> 2); } //Determine which ID corresponds to certain records switch (extraInfo[0]){ case 0: printf("%s ", "ID: Unkown"); break; break; case 1: printf("%s ", "ID: Friend"); break; case 2: printf("%s ", "ID: Foe"); break; case 3: printf("%s ", "ID: Neutral"); break; } switch (extraInfo[1]){ case 0: printf("%s ", "Category: Ship"); break; case 1: printf("%s ", "Category: Ground Vehicle"); break; case 2: printf("%s ","Category: Airplane"); break; } switch (extraInfo[2]){ case 0: printf("%s ", "Not Engaged"); break; case 1: printf("%s ", "Engaged"); break; }
} } //**PREVIOUSLY USED CODE** NO LONGER IN USE //for(int i=0;i
//for(int j=0;j
//}
fclose(file); return 0; }
Here is the output of that program:
latitude:-117.000000 longitude: 32.000000 altitude: 100 name: ABLE ID: Friend Category: Airplane Not Engaged latitude:-117.000000 longitude: 32.500000 altitude: 200 name: BAKER ID: Foe Category: Ground Vehicle Not Engaged latitude:-118.000000 longitude: 32.599998 altitude: 0 name: CAT ID: Neutral Category: Ship Not Engaged latitude:-120.000000 longitude: 33.000000 altitude: -10 name: DOWN ID: Foe Category: Ship Engaged
FIELD Latitude Longitude Altitude (in meters) Name Length in Bytes Type 4 4 Single precision float Single precision float Signed integer 5 characters-not necessarily null terminated. Unused characters are null. See Next Slide Misc MOST SIGNIFICANT BIT FIELD LEAST SIGNIFICANT BIT 0 VALUES Id 0-unknown, 1-friend, 2-foe, 3-neutral 0=ship. 1-ground vehicle, 2=airplane 0-not engaged 1-engaged Not used Category Engaged 4 4 Reserved 7 5 FIELD VALUES MOST SIGNIFICANTLEAST SIGNIFICANT BIT WORD BIT 0 Year 0 Month Day Hour Minute Reserved Second Reserved Since 2000. For example, 18 means 2018 1-12 1-31 0-23 0-59 0 0-60 0 10 15 0 0 0 10 15 1 1 0 15 FIELD Latitude Longitude Altitude (in meters) Name Length in Bytes Type 4 4 Single precision float Single precision float Signed integer 5 characters-not necessarily null terminated. Unused characters are null. See Next Slide Misc MOST SIGNIFICANT BIT FIELD LEAST SIGNIFICANT BIT 0 VALUES Id 0-unknown, 1-friend, 2-foe, 3-neutral 0=ship. 1-ground vehicle, 2=airplane 0-not engaged 1-engaged Not used Category Engaged 4 4 Reserved 7 5 FIELD VALUES MOST SIGNIFICANTLEAST SIGNIFICANT BIT WORD BIT 0 Year 0 Month Day Hour Minute Reserved Second Reserved Since 2000. For example, 18 means 2018 1-12 1-31 0-23 0-59 0 0-60 0 10 15 0 0 0 10 15 1 1 0 15Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started