I would like comments on the solution code so i can understa
I would like comments on the solution code so i can understand what it\'s doing please.
3. (30 pts) The binary file dates bin is shown in a hexadecimal editor 43 xv132 dates bin File Edit Search Address Bookmarks Iools XVIscript Help Ol07 0000 00 30 35 30 35 31 34 30 33 32 33 31 33 OOOO 0 50 5 1 40 3 2 3 1 3 la 10 30 31 32 3.5 31 34 30 38 31 36 31 32 30 37 33 310 1 2 5 1 40 8 1 6 1 2 0 7 3 1 20 31 34 30 32 30 36 31 32 31 31 32 33 31 35 30 39 1 4 0 2 0 61 2 l 1 2 3 1 5 0 9 l 4 l 3 30 31 34 31 33 Char dec: 7 Overwrite Adr. hex: 0 The file is full of dates. It starts with an integer (4 bytes) giving the number of dates. The dates are formatted as MMDDYY (two chars for the month, two chars for the day, and two chars for the year). Write a program that reads in this file (or any similarly formattted version of it) and stores all dates from 2014 into a new file called new dates. bin The new file is a binary file with the same format as dates. binSolution
struct is analogous to a class. there are differences but you can ignore for now.
here we have created a struct named date which has 3 arrays namely m,d,y each of size 2 and typr char.
we can create object of struct date.eg
strcut date todaysdate;
the arrays can be accessed as follows
todaysdate.m[0]
. FILE pointer to create and use FILE for different operations.
 
 syntax
 
 FILE* filePointerName;
eg.File*infile
fopen()
syntax
 
 fopen(“fileName”,”mode”);
eg.
file*infile=fopen(\"dates.bin\",rb)
rb=readmode for non text files
wb=write mode for non text files
fread()
It is used to read data from file.
syntax of the function is:
 
 fread (void* ptr, t size, t count,FILE* stream)
 
 Parameters
 
 ptr: pointer to memory where the read data will be stored.
 size: size of data elements (in byte) to be read.
 count: number of data elements to be read.
 stream: pointer to the file from which the data will be read.
 
 Return
 
 Type: t
 Value: On success number of elements which are successfully read.
 On failure returns zero (0) if the size or count is 0, Error indicator is set.
eg.in our case
fread returns the first integer and since as stated the first integer=number of dates in infile hence datecount.
then we create arrays namely dates and keeperdate both of size datecount
fread(dates,sizeof(struct date),datecount,infile)
reads all dates and stores it in array dates as objects of struct date
Note: fread skips those characters which do not obey the size specified , so second call to fread does not reads the datecount which is the first integer in your file.
the for loop.
iterates through dates array and
It checks if the year of date ==14 [since we have to only copy dates of 2014]
if(yes)
save that date in Keeper array and increment keepercount
fwrite().
It is used for file writing.
syntax is:
 
 fwrite(const void* ptr, t size,t count,FILE* stream)
 
 Parameters:
 
 ptr: pointer to the data which will be written.
 size: the size of data elements (in byte) to be written.
 count: number of data elements to be written.
 stream: the pointer to the file where the data will be written.
 
 Return
 
 Type: t
 Value: On success number of elements which are successfully written
 On failure zero (0) if the size or count is 0. Error indicator is set.
fwrite(&keppercount,sizeof(int)1,outfile);
it simply outputs datecount as first integer of file
fwrite(keeperdates,sizeof(struct date),keepcount,outfile)
outputs all date objects in keeperdates array into outfile.
return 0; to exit
Note: fwrite appends to end of file.


