Your task is to write a program that stores and retrieves structures in a file on disk. The file of structures must remain on the disk once your program ends. You should be able to store structures to the file and retrieve from the file after restarting the program.
The record that you will be writing to file has the following structure:
struct contact {
unsigned long phone_number;
long first_name_posn;
long last_name_posn;
long company_name_posn;
long email_posn;
long next;
};
first_name_posn, last_name_posn, company_name_posn, and email_posn are the position in the file of the First Name, Last Name, Company Name, and Email variable strings. These locations will be required for writing and reading the information to be stored.
When you write the structures on disk you will need to store the First Name, Last Name, Company Name, and Email strings separately from the contact structure. In the file you will write the contact structure first followed by the First Name, Last Name, Company Name, and Email strings (if they exist). The only required information is the Phone Number all other information is optional.
The position next stores the location in the file where the next contact record is stored.
The file can contain any number of records and associated strings. The name of the file will be myContactList.db and if the file does not exist then you program must create it.
Interface (input and output)
Your program will have an input interface that does the following:
Do you wish to enter a new contact (Yes or No)?:
First Name:
Last Name:
Company Name:
Phone Number (enter only numbers):
Email:
The program will move to the next stage when you answer No to the first question. The answer must be No exactly observe the upper case N and lower case o.
The second stage interface is as follows:
Do you wish to retrieve a contact (Yes or No)?:
Phone Number:
The program will end if you answer No to this question. If you answer Yes then it will ask for the phone number of a contact. Your program will search the file for the first record that satisfies this request. If it finds a match then it will output the following:
First Name: Deb
Last Name: Stacey
Company Name: University of Guelph
Phone Number (enter only numbers): 5198244120
Email: dastacey@uoguelph.ca
If it does not find a match it will output:
No match found.
In both cases, the interface will continue by going back to the first question - Do you wish to enter a new contact (Yes or No)?
The program will create the file myContactList.db if it does not exist upon program start up. If it does exist then it will be opened for reading and writing and the current file pointer should be positioned at the end of the file.
Example Session
$ ./contacts
Do you wish to enter a new contact (Yes or No)?: Yes
First Name: Deb
Last Name: Stacey
Company Name: University of Guelph
Phone Number (enter only numbers): 5198244120
Email: dastacey@uoguelph.ca
Do you wish to enter a new contact (Yes or No)?: Yes
First Name: Donald
Last Name: Duck
Company Name:
Phone Number (enter only numbers): 4162599373
Email: dduck@disney.com
Do you wish to enter a new contact (Yes or No)?: Yes
First Name:
Last Name:
Company Name: Ducks Unlimited
Phone Number (enter only numbers): 4162344567
Email: ducks@unlimited.ca
Do you wish to enter a new contact (Yes or No)?: No
Do you wish to retrieve a contact (Yes or No)?: Yes
Phone Number: 4161234456
No match found.
Do you wish to enter a new contact (Yes or No)?: No
Do you wish to retrieve a contact (Yes or No)?: Yes
Phone Number: 4162599373
First Name: Donald
Last Name: Duck
Company Name:
Phone Number (enter only numbers): 4162599373
Email: dduck@disney.com
Do you wish to enter a new contact (Yes or No)?: No
Do you wish to retrieve a contact (Yes or No)?: No
$
The file myContactsList.db will contain three records (Deb Stacey, Donald Duck, and Ducks Unlimited).
The file should be 261 bytes long and the command od cd myContactsList.db will produce the following:
0000000 030 351 326 5 001 \0 \0 \0 0 \0 \0 \0 \0 \0 \0 \0
59672 13782 1 0 48 0 0 0
0000020 4 \0 \0 \0 \0 \0 \0 \0 ; \0 \0 \0 \0 \0 \0 \0
52 0 0 0 59 0 0 0
0000040 P \0 \0 \0 \0 \0 \0 \0 e \0 \0 \0 \0 \0 \0 \0
80 0 0 0 101 0 0 0
0000060 D e b \0 S t a c e y \0 U n i v e
25924 98 29779 25441 31077 21760 26990 25974
0000100 r s i t y o f G u e l p h \0
29554 29801 8313 26223 18208 25973 28780 104
0000120 d a s t a c e y @ u o g u e l p
24932 29811 25441 31077 30016 26479 25973 28780
0000140 h . c a \0 315 9 034 370 \0 \0 \0 \0 225 \0 \0
11880 24931 52480 7225 248 0 38144 0
0000160 \0 \0 \0 \0 \0 234 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
0 0 39936 0 0 0 0 0
0000200 \0 \0 \0 \0 \0 241 \0 \0 \0 \0 \0 \0 \0 262 \0 \0
0 0 41216 0 0 0 45568 0
0000220 \0 \0 \0 \0 \0 D o n a l d \0 D u c k
0 0 17408 28271 27745 100 30020 27491
0000240 \0 d d u c k @ d i s n e y . c o
25600 30052 27491 25664 29545 25966 11897 28515
0000260 m \0 w V 030 370 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
109 22135 63512 0 0 0 0 0
0000300 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 342 \0 \0 \0 \0 \0
0 0 0 0 0 226 0 0
0000320 \0 \0 362 \0 \0 \0 \0 \0 \0 \0 005 001 \0 \0 \0 \0
0 242 0 0 0 261 0 0
0000340 \0 \0 D u c k s U n l i m i t e
0 30020 27491 8307 28245 26988 26989 25972
0000360 d \0 d u c k s @ u n l i m i t e
100 30052 27491 16499 28277 26988 26989 25972
0000400 d . c a \0
11876 24931 0
0000405
The data stored is as follows:
Contact Record 1: stored at file position 0
Contact Record 2: stored at file position 101
Contact Record 3: stored at file position 178
Other Information
Any of the fields can be blank except for Phone Number and Email. If the user does not type anything in these fields, then they will be asked for the information again. E.g.
…
Phone Number (enter only numbers):
Phone Number (enter only numbers): 4162599373