I'm able to import the file and read it, but I don't think it's saving to the structure. I want to figure out how to save it within the structure so I can search/ delete the contacts. The unfortunately lengthy code is below:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define BUFFSIZE 500
//Structure for contacts. These are now pointers.
typedef struct friends_contact{
char *First_Name;
char *Last_Name;
char *home;
char *cell;
}fr;
//Function declarations
void menu(fr*friends ,int* counter,int i,char buffer[]);
void setFirst(fr*,int *,int i,char buffer[]);
char getFirst(fr*,int i);
void setLast(fr*friends, int* counter, int i,char buffer[]);
char getLast(fr*friends , int i);
void setHome(fr*friends, int* counter, int i,char buffer[]);
char getHome(fr*friends, int i);
void setCell(fr*friends, int* counter, int i,char buffer[]);
char getCell(fr*friends, int i);
void add_contact(fr*friends,int* counter,int i,char buffer[]);
void print_contact(fr*friends ,int* counter, int i,char user_entry3[50]);
char delete_contact(fr*friends ,int* counter, int i);
int show_contact(fr*friends ,int* counter, int i);
void file(fr*friends ,int* counter, int i,char user_entry3[50]);
void file2 (fr*friends ,int* counter, int i,char buffer[],FILE*read);
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_entry1=0;
int user_entry2=0;
char user_entry3[50]={'\0'};
FILE *read;
printf("Welcome! Would you like to import a file? (1)Yes or (2) No");
scanf("%d",&user_entry1);
if(user_entry1==1)
{
printf("Please enter a file name");
scanf("%s",user_entry3);
read=fopen(user_entry3,"r");
}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);
if(user_entry1==1)
{
file2(friends ,counter,i,buffer,read);
}else;
}
}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!");
}
}
}
//Start of Set functions. Each entry has its own set function that gathers the data
//For each interaction, we have a buffer and a malloc.
void setFirst(fr*friends, int* counter, int i,char buffer[])
{
printf("Enter a first name \n");
scanf("%s",buffer);
friends[*counter].First_Name=malloc(BUFFSIZE*strlen(buffer));
strcpy(friends[*counter].First_Name, buffer);
}
void setLast(fr*friends, int* counter, int i,char buffer[])
{
printf("Enter a last name \n");
scanf("%s",buffer);
friends[*counter].Last_Name=malloc(BUFFSIZE*strlen(buffer));
strcpy(friends[*counter].Last_Name, buffer);
}
void setHome(fr*friends, int* counter, int i,char buffer[])
{
printf("Enter a home number \n");
scanf("%s",buffer);
friends[*counter].home=malloc(BUFFSIZE*strlen(buffer));
strcpy(friends[*counter].home, buffer);
}
void setCell(fr*friends, int* counter, int i,char buffer[])
{
printf("Enter a cell number \n");
scanf("%s",buffer);
friends[*counter].cell=malloc(BUFFSIZE*strlen(buffer));
strcpy(friends[*counter].cell, buffer);
}
//Start of Get functions. Each function sends the data to the executing function.
char getFirst(fr*friends , int pos)
{
printf("%s ", friends[pos].First_Name);
return *friends[pos].First_Name;
}
char getLast(fr*friends , int pos)
{
printf("%s\n", friends[pos].Last_Name);
return *friends[pos].Last_Name;
}
char getHome(fr*friends , int pos)
{
printf("(Home) ""%s\n", friends[pos].home);
return *friends[pos].home;
}
char getCell(fr*friends , int pos)
{
printf("(Cell) ""%s\n", friends[pos].cell);
return *friends[pos].cell;
}
//This function allows for the all the set functions to be added.
void add_contact(fr*friends,int* counter,int i,char buffer[])
{
setFirst(friends,counter,i,buffer);
setLast(friends,counter,i,buffer);
setHome(friends,counter,i,buffer);
setCell(friends,counter,i,buffer);
(*counter)++;
}
//This is used to delete a name out of the book.
char delete_contact(fr*friends ,int* counter, int i)
{
char name_search[50]={'\0'};
char Delete[5]={'\0'};
printf("Search by last name\n");
scanf("%s",name_search);//Name entry
for(i=0;i<*counter;i++)
{
if(strcmp(name_search,friends[i].Last_Name)==0)//Copys over the name entered
{
strcpy(friends[i].Last_Name,Delete);
}
}
//Freeing up memory.
free(friends[i].First_Name);
free(friends[i].Last_Name);
free(friends[i].home);
free(friends[i].cell);
printf("\nName(s) has been deleted\n");
}
//This function prints out all the contact information
void print_contact(fr*friends ,int* counter, int i,char user_entry3[50])
{
for( i = 0; i < *counter; i++)
if (strlen(friends[i].First_Name) && strlen(friends[i].Last_Name)&& strlen(friends[i].home)&& strlen(friends[i].cell ))
{
getFirst(friends, i);
getLast(friends, i);
getHome(friends, i);
getCell(friends, i);
}
}
//Displays the contact in which you are searching for.
int show_contact(fr*friends ,int* counter, int i)
{
char name_search2[50]={'\0'};
int flag=0;
printf("Please enter a last name\n");
scanf("%s",name_search2);
for(i=0;i<*counter;i++)
{
//If the name is found, it reaturns the contact info.Now works for duplicate last names.
if(strcmp(name_search2,friends[i].Last_Name)==0)
{
(strlen(friends[i].First_Name) && strlen(friends[i].Last_Name)&& strlen(friends[i].home)&& strlen(friends[i].cell ));
getFirst(friends, i);
getLast(friends, i);
getHome(friends, i);
getCell(friends, i);
flag++;
}
}
return flag;
}
void file(fr*friends ,int* counter, int i,char user_entry3[5])
{
FILE*fp;
char flag2;
fp=fopen(user_entry3,"w");
for( i = 0; i < *counter; i++)
{
if (strlen(friends[i].First_Name) && strlen(friends[i].Last_Name)&& strlen(friends[i].home)&& strlen(friends[i].cell ))
{
fprintf(fp,"\n""%s ",friends[i].First_Name);
fprintf(fp,"%s ""\n",friends[i].Last_Name);
fprintf(fp,"<Home>""%s""\n",friends[i].home);
fprintf(fp,"<Cell>""%s""\n",friends[i].cell);
}
}
fclose(fp);
}
void file2(fr*friends ,int* counter, int i,char buffer[],FILE*read)
{
fseek(read, 0, SEEK_SET);
while (fscanf(read,"%s", buffer) != EOF)
{
friends[*counter].Last_Name=malloc(BUFFSIZE*strlen(buffer));
strcpy(friends[*counter].Last_Name, buffer);
printf("%s\n",friends[*counter].Last_Name);
}
}
I understand the code isn't perfect, and might have a few flaws that aren't related to this question, but I've been trying for a while to figure this issue out. It would be greatly appreciated if someone could help me out !