狗数据库的错误修复

时间:2016-03-15 21:01:00

标签: c database

我正在开发一个狗数据库,并且有一个我无法弄清楚的错误。 错误是:Thread 1: signal SIGABRT. 每当我尝试搜索或更换狗时,我都会收到此错误代码。

到目前为止我的代码:

 #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");
 }

1 个答案:

答案 0 :(得分:2)

第161行的find_dog()

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

rec已经是一个指针,所以你不应该拿它的地址。

你在第126行view_dog()遇到了同样的问题。

我通过运行一个名为Valgrind的程序找到了这一点,如果你是一名C程序员,你应该明白并学习如何使用它。