I have a phonebook app that I am trying to give the functionality of being able to read and write files. I’ve figured out pretty easily how to write files, but reading them has really got me stuck. I think my main issue is the inability to get the file to loop(typically crashes as soon as it hits the loop). Below is my my effected functions.
Here are the structure, main, and menu functions.
typedef struct friends_contact{
char *First_Name;
char *Last_Name;
char *home;
char *cell;
}fr;
int main() {
fr friends[5];
char buffer[BUFFSIZE];
int counter=0;
int i=0;
menu(friends, &counter,i,buffer);
getch();
return 0;
}
//Menu function
void menu(fr*friends,int* counter, int i,char buffer[]) {
int user_entry=0;
int user_entry2=0;
char user_entry3[50]={'\0'};
printf("Welcome! Would you like to import a file? (1)Yes or (2) No");
scanf("%d",&user_entry);
if(user_entry==1)
{
file2(friends,counter,i,user_entry3);
}else;
do{
int result;
printf("\nPhone Book Application\n");
printf("1) Add friend\n2) Delete friend\n3) Show a friend\n4) Show phonebook\n5)Exit\n");
scanf("%d", &user_entry);
if(user_entry==1)
{
add_contact(friends,counter,i,buffer);
}
if(user_entry==2)
{
delete_contact(friends ,counter,i);
}
if(user_entry==3)
{
result=show_contact(friends ,counter,i);
if(result==0){
printf("\nName not Found\n");
}else{
result;
}
}
if(user_entry==4)
{
print_contact(friends, counter,i,user_entry3);
file2(friends ,counter,i,user_entry3);
}
}while(user_entry!=5);
if(user_entry==5)
{
printf("Would you like to save entries to a file? (1)yes or (2) no");
scanf("%d",&user_entry2);
if(user_entry2 == 1)
{
printf("Please name your file");
scanf("%s",user_entry3);
file(friends, counter,i,user_entry3);
printf("Goodbye!");
}else if(user_entry2 == 2){
printf("Goodbye!");
}
}
}
Here is the function that handles the reading of the file.
void file2(fr*friends ,int* counter, int i, char user_entry3[50])
{
FILE *read;
printf("Please enter a file name");
scanf("%s",user_entry3);
read=fopen(user_entry3,"r");
//This is where the crash is taking place!!**
while(!feof(read)){
fscanf(read,"%s %s %s %s",friends[i].First_Name,friends[i].Last_Name,friends[i].home,friends[i].cell);
printf("\n""%s ""%s ""\n""<Home>""%s""\n""<Cell>""%s""\n",friends[i].First_Name,friends[i].Last_Name,friends[i].home,friends[i].cell);
}
Now I understand there may be other problems with the program unrelated to what I’m asking, but I am new to C so this is a work in progress. I need to figure out how to stop this from crashing, and how to add this to the rest of my contacts(which I think I might have an idea for), it’s driving me crazy! thanks in advance.
You are reading data into undefined memory regions (you never assign any value to the 4 string pointers in your struct). I don’t see you allocating memory for
friends[i].First_Name,.Last_Name,.homeand.cell.You might want to change your struct like this:
Of course, if the file contains a part longer then 50 characters (including ‘\0’ termination), your code will crash again, because
fscanfwill not check for the length unless you specify the maximum length for each string like this:If you want to use pointers in your struct, you should allocate memory to each struct member using
malloc()before the reading and usefree()to release the allocated memory once you don’t need it anymore.