Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3677694 • Letter: H

Question

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 user’s 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 dog’s 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<stdio.h>
#include<string.h>
#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 Modify Name: ", record[index].name);
    scanf("%s", record[index].name);
  
    printf(" Old Value: %s Modify Breed: ", record[index].breed);
    scanf("%s", record[index].breed);
  
    printf(" Old Value: %s Modify Color: ", record[index].color);
    scanf("%s", record[index].color);
  
    printf(" Old Value: %.2f Modify Weight: ", record[index].weight);
    scanf("%f", &record[index].weight);
  
    printf(" Old Value: %d Modify Age: ", record[index].age);
    scanf("%d", &record[index].age);
  
    printf(" Old Value: %s Modify Sex: ", record[index].sex);
    scanf("%s", record[index].sex);
}

//Function that displays records
void viewRecord(int total)
{
    int j;
  
    for(j=0;j<total;j++)
    {
        printf(" %s %s %s %.2f %d %s", record[j].name, record[j].breed, record[j].color, record[j].weight, record[j].age, record[j].sex);
        printf(" ");
    }
}

//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<n;j++)
    {
        if(strcmp(record[j].name,dog)==0)
            flag=1;
    }
  
    if(flag==1)
        printf("Record exists ");
    else
        printf("No such record exists ");
}

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#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];
char pr_menu(void);
void addRecord(struct dog_entry *reg, int type);
void modifyRecord(int);
void delete_dog(int g, struct dog_entry *rec);
void view_dog(int, struct dog_entry *reg);
off_t find_dog(int);
void searchRecord(int, struct dog_entry *rec);


int main()
{
struct dog_entry reg;
int ch;
int i=0,j,n;
while(true)
{
char ch =pr_menu();
  
switch(ch)
{
case '1':
addRecord(&reg,1);
i++;
break;
  
case '2':
addRecord(&reg,2);
break;
  
case '3': delete_dog(1,&reg);
break;
  
case '4': view_dog(1,&reg);
break;
  
case '5': searchRecord(n,&reg);
break;
  
default: break;
}
  
if (ch == '6') // exit
break;

}
system("pause");
}

char pr_menu(void)
{
char ch;
system("cls");
printf(" Menu: 1.Add 2.Change 3.Delete 4.View 5.Search 6. Exit Enter Choice: ");
scanf("%c",&ch);
return ch;
}

//Function that adds a record
void addRecord(struct dog_entry *rec, int type)
{
FILE * f;//define donde se guarda el archivo
f=fopen("database_dog.txt", "a+");//definition of file

system("cls");
printf("Add the Dog: ");
  
printf(" Enter Name: ");
scanf("%s", rec->name);
  
printf(" Enter Breed: ");
scanf("%s", rec->breed);
  
printf(" Enter Color: ");
scanf("%s", rec->color);
  
printf(" Enter Weight: ");
scanf("%f", &rec->weight);
  
printf(" Enter Age: ");
scanf("%d", &rec->age);
  
printf(" Enter Sex: ");
scanf("%s", rec->sex);
  
fseek(f, 0, SEEK_END);
fwrite(&rec, sizeof(struct dog_entry),1,f);//save the data
fclose(f);//close the file
printf(" ");
system("pause");
}
//Function that modifies the record
void modifyRecord(int index)
{
  
}
//Function that displays records
void view_dog(int total, struct dog_entry *rec)
{
FILE * f;//define donde se guarda el archivo
f=fopen("database.dog", "r+");//definition of file
char name [SZ_NAME];
  
system("cls");
printf("View the Dog: ");
printf(" Enter Dog's Name: ");
scanf("%s", name);
  
rewind(f);
while(fread(&rec, sizeof(struct dog_entry),1, f))
{ //apertura del while
if(strcmp(name,rec->name)==0)//compara una cadena de caracteres
{
printf(" Name: %s", rec->name);
  
printf(" Breed: %s", rec->breed);
  
printf(" Color: %s", rec->color);
  
printf(" Weight: %f", &rec->weight);
  
printf(" Age: %d", &rec->age);
  
printf(" Sex: %s", rec->sex);
  
printf(" ");
system("pause");
break;
}
}
  
fclose(f);//close the file
  
}

int find_dog(int g, struct dog_entry *rec){
FILE * f;//define donde se guarda el archivo
f=fopen("database.dog", "r+");//definition of file
char name [SZ_NAME];
  
printf(" Enter Dog's Name: ");
scanf("%s", name);
  
rewind(f);
int c;
while(fread(&rec, sizeof(struct dog_entry),1, f))
{ //apertura del while
if(strcmp(name,rec->name)==0)//compara una cadena de caracteres
{
c++;
break;
}
}
  
fclose(f);//close the file
return c;
}
void delete_dog(int g, struct dog_entry *rec){
FILE * f;//define donde se guarda el archivo
f=fopen("database.dog", "r+");//definition of file
char name [SZ_NAME];
int num=0;
  
system("cls");
printf("Delete Record: ");
printf(" Enter Dog's number: ");
scanf("%d", &num);
  
fseek(f, num*sizeof(struct dog_entry), SEEK_SET);
fread(&rec, sizeof(struct dog_entry), 1, f);
fclose(f);//close the file
}
//Function that searches for the record
void searchRecord(int n, struct dog_entry *rec)
{
char dog[SZ_NAME];
int j;
int flag=0;
  
flag = find_dog(0, rec);
  
if(flag==1)
printf("Record exists ");
else
printf("No such record exists ");
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote