Bug fix for Dog Database

I am working on a dog database and have one bug I cannot figure out. The bug is: Thread 1: signal SIGABRT. Every time I try to search or change for a dog, I get this error code.

My code so far:

 #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);
  int find_dog(int, struct dog_entry *rec);
  void searchRecord(int, struct dog_entry *rec);

  int main()
  {
struct dog_entry reg;
int i = 0, n;
while(free)
{
    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("\n Menu:\n 1.Add\n 2.Change\n 3.Delete\n 4.View\n 5.Search\n 6.Exit\n      \nEnter 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", "w+");//definition of file

// system("cls");
printf("Add the Dog: ");

printf("\n Enter Name: ");
scanf("%s", rec->name);

printf("\n Enter Breed: ");
scanf("%s", rec->breed);

printf("\n Enter Color: ");
scanf("%s", rec->color);

printf("\n Enter Weight: ");
scanf("%f", &rec->weight);

printf("\n Enter Age: ");
scanf("%d", &rec->age);

printf("\n 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("\n\n");
// 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.txt", "r+");//definition of file
char name [SZ_NAME];

// system("cls");
printf("View the Dog: ");
printf("\n 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("\n Name: %s", rec->name);

        printf("\n Breed: %s", rec->breed);

        printf("\n Color: %s", rec->color);

        printf("\n Weight: %f", rec->weight);

        printf("\n Age: %d", rec->age);

        printf("\n Sex: %s", rec->sex);

        printf("\n\n");
        // 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.txt", "r+");//definition of file
char name [SZ_NAME];

printf("\n 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.txt", "r+");//definition of file
  char name [SZ_NAME];
  int num = 0;

// system("cls");
printf("Delete Record: ");
printf("\n 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\n");
else
    printf("No such record exists\n");
 }

Answers


In find_dog() on line 161:

fread(&rec, sizeof(struct dog_entry), 1, f);

rec is already a pointer, so you shouldn't take its address.

You've got the same problem in view_dog() on line 126.

I found this out by running a program called Valgrind, which you should definitely get and learn how to use if you are a C programmer.


Need Your Help

Remote desktop is stuck on "securing remote connection"

azure remote-desktop rdp

Remote desktop is stuck on "securing remote connection". I am connecting to Windows 2008 VM on Azure from Windows 7. I have successfully connected for a few days before now. I have waited for over 15

Changing mouse pointer on load of page to a specific div

javascript php jquery html

Is is possible to change the mouse pointer(x,y) to some (x2,y2) location when a page is loaded? I want to move my mouse pointer on a particular div, when the page is loaded.