Question
PROMPT: In this exercise you are to create a database of dogs in a file, using open(), close (), read (), write (), and lseek
PROMPT:
In this exercise you are to create a database of dogs in a file, using open(), close (), read (), write (), and lseek (). Do NOT use the standard library fopen () ... calls! Be sure to follow the directions! The database file (database.dog) will be an array of records (structures): #define SZ_NAME 32 #define SZ_BREED 32 #define SZ_COLOR 32 struct dog_entry { char name [SZ_NAME]; char breed [SZ_BREED]; char color [SZ_COLOR]; unsigned short weight; unsigned char age; char sex; }; #define REC_SIZE sizeof(struct dog_entry) // should be 100 Note: be sure to leave room in each char array for a null byte at the end, and do NOT overwrite the arrays! If the user enters 32 or more characters, you must truncate the entry to 31 characters, terminated by a null byte. Present the user with the following menu: Enter 1 to add, 2 to change, 3 to delete, 4 to view, 5 to search, 6 to exit Enter:
You should implement all of the following functions (with suggested prototypes): unsigned char pr_menu(void); void add_dog(int, off_t); // passed an fd and offset in file int del_dog(int, off_t); // ditto; returns new fd void view_dog(int); // passed an fd off_t find_dog(int); // passed an fd, returns offset into file
The pr_menu function prints the menu, validates the users input and returnsthe option chosen as an unsigned char. The database.dog file should beopened in the main routine and its fd is then passed to all of the otherroutines. The view_dog function displays all of the dogs in the database. Thefind_dog function searches the database given a dogs name (which must beunique in the database) and returns the offset to the beginning of the record. The add dog and change dog options should use add_dog function to prompt the user for the contents of a dog_entry record and write that record into the file at the passed offset. The add option always places the new record at the end of the database; the change option replaces the record somewhere in the existing database, and overwrites the previous record. You will need to use the lseek (2) function to move around inside the open file. In the add_dog function you may want to use the strlen (3), strcpy (3), and fgets (3) library functions. The fgets function is used to read an entire line: fgets (buffer, buf_size, stdin); be sure to read its man pages carefully. You need to deal effectively with users input EXCEEDING the length of the char arrays in the structure (you must read the entire line of input and may have to truncate it in the structure). To inspect the contents of the database.dog file (which is NOT all text) use od c as in Step 5 in Assignment 1. The delete option is up to you to design, but there are some hints embedded in the prototypes shown.
Step 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