. Hallo everyone, i'm new in here and also new in c (3 months).
I tryed to make a db in C and the program works fine but i have an issue when i remove the db file (database.txt), i get Segmentation fault (core dumped)
The program is:
#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);
    }
}
The db file(database.txt) is:
john doe 1880 1234567
mike michael 1850 7654321
george hartman 1971 2345678
david russo 1982 8765432
michael jackson 1960 3456789
. When i run it looks like this: 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
But if i delete the file, i get:
Segmentation fault (core dumped)
In my opinion i think that i have to make a check to see if the file exist, if the file not exist to print the error. Something like this:
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;
    }
}
But i don't know how. Thank You all
 
     
     
    