Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello I am working on a dog database. My assignment is as follows: Please follow these rukes which were laid out for me. In this

Hello I am working on a dog database. My assignment is as follows:

Please follow these rukes which were laid out for me. 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:

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)

Note: be sure to leave room in each char array for a null byte at the end, and do NOT overwrite the array!

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

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);

off_t find_dog(int); // returns offset into the file

The pr_menu function prints the menu, validates the users input and returns the option chosen as an unsigned char. The database.dog file should be opened in the main routine and its fd is then passed to all of the other routines. The view_dog function displays all of the dogs in the database. The find_dog function searches the database given a dogs name (which must be unique 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. 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), andfgets(3) library functions. The fgets function is used to read an entire line:fgets(buffer, buf_size, stdin); it is covered later but you need it for this exercise.

Please help me get this as CLOSE as possible to what the assignment is. Im at my wits end!

My program so far:

#include #include #define SZ_NAME 32 #define SZ_BREED 32 #define SZ_COLOR 16 #define SZ_SEX 8

struct dog_entry { char name [SZ_NAME]; char breed [SZ_BREED]; char color [SZ_COLOR]; float weight; int age; char sex [SZ_SEX]; };

#define REC_SIZE sizeof(struct dog_entry)

struct dog_entry record[REC_SIZE];

void addRecord(int); void modifyRecord(int); void viewRecord(int); void searchRecord(int);

int main() { int ch; int i=0,j,n,c=1; do { printf(" Enter Choice: 1.Add 2.Change 3.View 4.Search "); scanf("%d",&ch); switch(ch) { case 1: addRecord(i); i++; n=i; break; case 2: printf(" Enter record index to change "); scanf("%d",&j); modifyRecord(j); break; case 3: viewRecord(n); break; case 4: searchRecord(n); break; default: break; } printf(" Enter 1 to use dog database, 0 to exit "); scanf("%d",&c); }while(c==1); }

//Function that adds a record void addRecord(int index) { printf(" Enter Name: "); scanf("%s", record[index].name); printf(" Enter Breed: "); scanf("%s", record[index].breed); printf(" Enter Color: "); scanf("%s", record[index].color); printf(" Enter Weight: "); scanf("%f", &record[index].weight); printf(" Enter Age: "); scanf("%d", &record[index].age); printf(" Enter Sex: "); scanf("%s", record[index].sex); }

//Function that modifies the record void modifyRecord(int index) { printf(" Old Value: %s \t Modify Name: ", record[index].name); scanf("%s", record[index].name); printf(" Old Value: %s \t Modify Breed: ", record[index].breed); scanf("%s", record[index].breed); printf(" Old Value: %s \t Modify Color: ", record[index].color); scanf("%s", record[index].color); printf(" Old Value: %.2f \t Modify Weight: ", record[index].weight); scanf("%f", &record[index].weight); printf(" Old Value: %d \t Modify Age: ", record[index].age); scanf("%d", &record[index].age); printf(" Old Value: %s \t Modify Sex: ", record[index].sex); scanf("%s", record[index].sex); }

//Function that displays records void viewRecord(int total) { int j; for(j=0;j

//Function that searches for the record void searchRecord(int n) { char dog[SZ_NAME]; int j; int flag=0; printf(" Enter name to search "); scanf("%s",dog); for(j=0;j

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

2nd Edition

0470624701, 978-0470624708

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago