c语言数据库

Database in c programming

。 大家好, 我是这里的新人,也是 c 的新人(3 个月)。

我尝试在 C 中创建一个数据库,程序工作正常,但是当我删除数据库文件 (database.txt) 时遇到问题,我得到 分段错误(核心已转储)

节目是:

#include<stdio.h>
#include<string.h>

#define KRED  "\x1B[31m"
#define KYEL  "\x1B[33m"
#define RESET "3[0m"
#define TRUE 1
#define FALSE 0

struct{
    int born;
    char firstName[20];
    char lastName[20];
    double phoneNumber;
    int record;
}dataBase;

void showRecord(void);
void noRecord(char record[]);
void searchString(FILE *file, char string[]);

int main(void){
    char name[20];
    const char *db = "database.txt";
    FILE *file = fopen(db, "r");
    dataBase.record=1;

    printf("Please type First or Last name to search:\t");
    scanf("%s", name);

    searchString(file, name);
    fclose(file);
   return 0;
}

void showRecord(void){
    printf("\n\t\tRecord\t%d\n",dataBase.record++);
    printf("First Name:\t%s\n", dataBase.firstName);
    printf("Last Name:\t%s\n", dataBase.lastName);
    printf("Born:\t\t%d\n", dataBase.born);
    printf("Phone Number:\t%.0f\n", dataBase.phoneNumber);
    printf("------------------------------\n");
}

void noRecord(char record[]){
    printf(KRED "\n\t\t\tNo record found with name\t"RESET KYEL "%s\n"RESET , record);
}

void searchString(FILE *file, char string[]){
    int check = 0;

    while(!feof(file)){
      fscanf(file,"\n%s\t%s\t%d\t%lf\t",dataBase.firstName,dataBase.lastName,&dataBase.born,&dataBase.phoneNumber);

        if(strcasecmp(dataBase.firstName,string)==0){
             showRecord();
             check = TRUE;
        }else if(strcasecmp(dataBase.lastName,string)==0){
             showRecord();
             check = TRUE;
        }
    }
    if(check == FALSE) {
        noRecord(string);
    }
}

数据库文件(database.txt)是:

约翰·多伊 1880 1234567

mike michael 1850 7654321

george hartman 1971 2345678

david russo 1982 8765432

michael jackson 1960 3456789

。 当我 运行 它看起来像这样: 1

Please type First or Last name to search:   michael

        Record  1
First Name: mike
Last Name:  michael
Born:       1850
Phone Number:   7654321
------------------------------

        Record  2
First Name: michael
Last Name:  jackson
Born:       1960
Phone Number:   3456789
------------------------------

2

Please type First or Last name to search:   jack

            No record found with name   jack

但是如果我删除文件,我得到:

Segmentation fault (core dumped)

在我看来,我认为我必须检查文件是否存在,如果文件不存在则打印错误。 像这样:

if(file == NULL){
    printf("\n\n\t\t\tOOPS, Fisierul nu exista\n\n");
    break;
}else{
while(!feof(file)){
  fscanf(file,"\n%s\t%s\t%d\t%lf\t",dataBase.firstName,dataBase.lastName,&dataBase.born,&dataBase.phoneNumber);

    if(strcasecmp(dataBase.firstName,string)==0){
         showRecord();
         check = TRUE;
    }else if(strcasecmp(dataBase.lastName,string)==0){
         showRecord();
         check = TRUE;
    }
}

但我不知道怎么做。 谢谢大家

我会检查文件是否存在:

int main(){
    char name[20];
    char *db = "database.txt";

    if ((FILE *file = fopen(db, "r")) == NULL)
    {
        printf ("Error\n");
    }

    dataBase.record=1;

    printf("Please type First or Last name to search:\t");
    scanf("%s", name);

    searchString(file, name);
    fclose(file);
   return 0;
}

我还建议你看看这个(比如在你的问题的评论中提到):Why is “while ( !feof (file) )” always wrong?

编辑:

我对你的代码做了一些改进,但我没有测试它。

#include<stdio.h>
#include<string.h>

#define KRED  "\x1B[31m"
#define KYEL  "\x1B[33m"
#define RESET "3[0m"
#define TRUE 1
#define FALSE 0

struct
{
  int born;
  char firstName[20];
  char lastName[20];
  double phoneNumber;
  int record;
} dataBase;

void showRecord()
{
  printf("\n\t\tRecord\t%d\n",dataBase.record++);
  printf("First Name:\t%s\n", dataBase.firstName);
  printf("Last Name:\t%s\n", dataBase.lastName);
  printf("Born:\t\t%d\n", dataBase.born);
  printf("Phone Number:\t%.0f\n", dataBase.phoneNumber);
  printf("------------------------------\n");
}

void noRecord(char record[])
{
    printf(KRED "\n\t\t\tNo record found with name\t"RESET KYEL "%s\n"RESET , record);
}

void searchString(FILE *file, char *filename){
  int check = 0;

  // You need the filename to newly open the file!
  if ((FILE *checkFile = fopen(filename,"r")) == NULL)
  {
    printf("\n\n\t\t\tOOPS, the File doesn't exist\n\n");

    // Close the file, we don't need it any more.
    fclose (checkFile);

    // EXIT the programm on error
    exit (1);
  }
  else
  {
    // close the file, we don't need it any more.
    fclose (checkFile);
    // You still need to get rid of while(!feof(file))
    while (!feof(file) && !ferror(file)) 
    {
      fscanf(file,"\n%s\t%s\t%d\t%lf\t",dataBase.firstName,dataBase.lastName,&dataBase.born,&dataBase.phoneNumber);

      if (strcasecmp(dataBase.firstName,string)==0)
      {
        showRecord();
        check = TRUE;
      }
      else if (strcasecmp(dataBase.lastName,string)==0)
      {
        showRecord();
        check = TRUE;
      }
    }
  }

  if(check == FALSE)
  {
    noRecord(string);
  }
}

int main(){
    char name[20];
    char *db = "database.txt";

    // Check if file exists on first open!
    if ((FILE *file = fopen(db, "r")) == NULL)
    {
      printf("Error: File doesn't exist'");
      exit(1);
    }

    dataBase.record=1;

    printf("Please type First or Last name to search:\t");
    scanf("%s", name);

    searchString(file, name);
    fclose(file);
   return 0;
}

如果 fopen 失败,请退出程序。
fscanf 将 return 成功扫描的项目数。只要阅读了四个项目,就继续阅读文件。
在 scanf 格式中,%19s 将防止覆盖缓冲区。

#include<stdio.h>
#include<string.h>

#define KRED  "\x1B[31m"
#define KYEL  "\x1B[33m"
#define RESET "3[0m"
#define TRUE 1
#define FALSE 0

struct{
    int born;
    char firstName[20];
    char lastName[20];
    double phoneNumber;
    int record;
}dataBase;

void showRecord(){
    printf("\n\t\tRecord\t%d\n",dataBase.record++);
    printf("First Name:\t%s\n", dataBase.firstName);
    printf("Last Name:\t%s\n", dataBase.lastName);
    printf("Born:\t\t%d\n", dataBase.born);
    printf("Phone Number:\t%.0f\n", dataBase.phoneNumber);
    printf("------------------------------\n");
}

void noRecord(char record[]){
    printf(KRED "\n\t\t\tNo record found with name\t"RESET KYEL "%s\n"RESET , record);
}

void searchString(FILE *file, char string[]){
    int check = 0;

    while( ( fscanf(file," %19s %19s %d %lf",dataBase.firstName,dataBase.lastName,&dataBase.born,&dataBase.phoneNumber)) == 4) {
        //read from file as long as fscanf return four successfully scanned items
        if(strcasecmp(dataBase.firstName,string)==0){
             showRecord();
             check = TRUE;
        }else if(strcasecmp(dataBase.lastName,string)==0){
             showRecord();
             check = TRUE;
        }
    }
    if(check == FALSE) {
        noRecord(string);
    }
}

int main(){
    char name[20];
    char *db = "database.txt";
    FILE *file;
    if ( ( file = fopen(db, "r")) == NULL) {
        printf ( "%s does not exist\nCreate it and try again\n", db);
        return 1; //exit the program
    }
    dataBase.record=1;

    printf("Please type First or Last name to search:\t");
    scanf("%19s", name);

    searchString(file, name);
    fclose(file);
   return 0;
}