Write a program that stores and retrieves structures in a file on disk. The file
ID: 3861352 • Letter: W
Question
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 Noto 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 Noto 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 thenit will output the following:
First Name: Jack
Last Name: Steves
Company Name: Jack’s Business
Phone Number (enter only numbers): 6044015214
Email:jackS mal.com
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: Jack
Last Name: Steves
Company Name: Jack’s Business
Phone Number (enter only numbers): 6044015214
Email: jackS mal.com
Do you wish to enter a new contact (Yes or No)?: Yes
First Name: William
Last Name: Wilson
Company Name: Wilson’s Sport Company
Phone Number (enter only numbers): 8008745930
Email:wilson wilson.com
Do you wish to enter a new contact (Yes or No)?: Yes
First Name:
Last Name:
Company Name: Sports equipment
Phone Number (enter only numbers): 9187189876
Email:sport equip.com
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: 8008855930
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: 8008745930
First Name: William
Last Name: Wilson
Company Name: Wilson’s Sport Company
Phone Number (enter only numbers): 8008745930
Email:wilson wilson.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
59672 13782 1 048 0 0
00000020 4 ;
52 0 0 0 59 0 0
00000040 P e
80 0 0 0 101 0 0
00000060D e b J a c k B u I
2592498 29779 25441 31077 21760 26990 25974
0000100r n e s s c o m p a n y
29554 29801 8313 26223 18208 25973 28780 104
0000120 g m a I l c o m24932 29811 25441 31077 30016 26479 25973 2878
00000140h . c a 315 9 034 370 225 880 24931 524807225 248 0 38144
00000160 234 0 39936 0 0 0 0
00000200 241 262 0 41216 0 0 0 45568 00000220 W I l s o n c o m p a n y 0 17408 28271 27745 100 30020 27491
0000240 d s p o r t
25600 30052 27491 25664 29545 25966 11897 28515
0000260m w V 030 3709 22135 63512 0 0 0 0 00000300 342 0 0 0 0 226 0
00000320 362 005 001 242 0 0 0 261 0
00000340 s p o r t e q u i e0 30020 274918307 28245 26988 26989 25972
0000360d s p o r t e q u I p100 30052 27491 16499 28277 26988 26989 25972
0000400d . c a 11876 24931 0 0000405
The data stored is as follows:
Contact Record 1: stored at file position 0
•Phone Number: 6044015214
•First Name Position: 48
•Last Name Position: 54
•Company Name Position: 60
•Email Position: 82
•Next: 95
Contact Record 2: stored at file position 95
•Phone Number: 8008745930
•First Name Position: 126
•Last Name Position: 135
•Company Name Position: 145
•Email Position: 161
•Next: 179
Contact Record 3: stored at file position 178
•Phone Number: 9187189876
•First Name Position: 0
•Last Name Position: 0
•Company Name Position: 198
•Email Position: 226
•Next: 234
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.
Explanation / Answer
Exit(1);
}}}
}
This program is written in C language. fwrite() and fread() functions are used to write and read the files rescpectively. this program writes the records into mycontacts.dat file and retrives the contacts from that file.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.