Alright to add the ability to import files into a phonebook app that I created, but my issue im having is when I just add a counter to loop through the function that pulls the data from the file, it prints out names many different times, in different orders. I tried to add a while(!feof()) function, but when I did this the program crashes as soon as it reaches that function. As far as I can tell, I'm using it exactly how it's supposed to be used, but clearly I'm making a mistake somewhere. PLEASE HELP!

#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 user_entry3[50]);

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)
     {
       printf("please enter the name of the file");
       scanf("%s",user_entry3);  
       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);
   } 
   }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);
            file2(friends,counter,i,user_entry3);
    }
}
//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 user_entry3[50])
{

     FILE *read;
     read=fopen(user_entry3,"r");
     //Only seems to work if I add a for(i=0;i<*counter;i++), but this gives me the bad results.
     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);

          }


}

I've attatched the entire code, it's kind of long, but hopefully someone can figureo out what I did wrong.

ps. I know there is probably other errors that are unrealated to this question, but the program is a work in progress.

line 288: Where is the memory being allocated for the members of that structure? All I see is unallocated pointers, which is why your program crashes when it tries to execute that scanf(). scanf() doesn't allocate memory, you have to do that yourself before calling scanf(). One way to fix the problem is to use temp buffers for scanf() then allocate memory needed and copy to the structure members.

char lname[40], fname[40], ... // etc for each item
fscanf(read,"%s%s...", lname, fname ...)
friends.first_name = malloc(strlen(fname)+1);
strcpy(friends.first_name,fname);
// etc for each structure member

Don't forget to free() the memory somewhere in your program to avoid memory leaks.

Edited 4 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.