gpt4 book ai didi

c - C语言编程中的数据库

转载 作者:行者123 更新时间:2023-11-30 18:54:51 28 4
gpt4 key购买 nike

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

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

程序是:

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

#define KRED "\x1B[31m"
#define KYEL "\x1B[33m"
#define RESET "\033[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;
}
}

但是我不知道怎么办。谢谢大家

最佳答案

如果fopen失败,则退出程序。
fscanf 将返回成功扫描的项目数。继续读取文件,直到读取了四个项目。
在 scanf 格式中 %19s 将防止覆盖缓冲区。

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

#define KRED "\x1B[31m"
#define KYEL "\x1B[33m"
#define RESET "\033[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;
}

关于c - C语言编程中的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29429642/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com