Saving read file data into structure

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 3230 Views - Last Post: 20 November 2012 - 02:05 AM Rate Topic: -----

#16 jcmoney1010  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-October 12

Re: Saving read file data into structure

Posted 19 November 2012 - 02:23 AM

View PostAdak, on 19 November 2012 - 02:16 AM, said:

I didn't do the home and cell struct members. They're more of the same logic.


Hmm... from what I can tell I implemented the code correctly, but this is my output for when I try and see the name within the phonebook list.
 "
2293468. x☺>
 "


Was This Post Helpful? 0
  • +
  • -

#17 jcmoney1010  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-October 12

Re: Saving read file data into structure

Posted 19 November 2012 - 02:49 AM

Alright, I have it working kinda lol. It reads the file, brings up the info and I can even delete it, but it doesn't save the name back to a file if that option is chosen, nor does the name come up if it is searched for. Kind of weird how the delete function worked, but not the other 2
Was This Post Helpful? 0
  • +
  • -

#18 jcmoney1010  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-October 12

Re: Saving read file data into structure

Posted 19 November 2012 - 02:57 AM

After further testing, I discovered that it wasn't really deleting the name, but rather since the file was closing after the first time it prints, it was simply just erasing the contact after the first time viewing it. I'm Back to the drawing board.
Was This Post Helpful? 0
  • +
  • -

#19 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Saving read file data into structure

Posted 19 November 2012 - 04:16 AM

The reading of the home and cell fields really caused me to change the readFile() function around.

New code:

/*
John Smith
<Home> 8
<Cell> 9
Sue Dyer
<Home> 3
<Cell> 2
Claire Barton
<Home> 1
<Cell> 5
Rob Clearsky
<Home> 4
<Cell> 6
Billy Bestfriend
<Home> 7
<Cell> 10
*/
#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 *fname;
   char *lname;
   char *home;
   char *cell;
}fr;
//Function declarations 
/*
In these functions, if you aren't going to change the counter, you should not have a 
pointer to counter. After you send a pointer to a function, if you subsequently call 
another function (like printfile), send the *counter - because that is NOT a pointer, anymore.
That's the number.
*/

void menu(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 readFile (fr*friends ,int* counter, int i,char buffer[],FILE*read);
char printfile(fr*friends ,int counter);

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]={"phonebook.txt"};
   FILE *fpIn;
   fpIn=fopen(user_entry3,"r+"); //you have to have some data in the phonebook
   //printf("Welcome! Would you like to import a file? (1)Yes or (2) No");
   //scanf("%d",&user_entry1);
   readFile(friends ,counter, i,buffer,fpIn);
   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==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){
            readFile(friends ,counter,i,buffer,fpIn);
         }else;
      } 
    
   }while(user_entry!=5);
   
}

//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].lname)==0)//Copys over the name entered
      {
         strcpy(friends[i].lname,Delete);
      }
   }
   //Freeing up memory.
   free(friends[i].lname);
   printf("\nName(s) has been deleted\n");
   return 'a';             
}
//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].fname) && strlen(friends[i].lname)&& strlen(friends[i].home)&& strlen(friends[i].cell ))
      {
      }
}
//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].lname)==0)
      {  /*for alignment, structs are padded. You can't add up struct members and say that will always be the size
         of the struct - use sizeof(nameOfStruct), in the function you create the struct instance 
         Also, different PC's may give a struct a different size because they use different padding. 
         but sizeof(struct) will be accurate on all. */
         
         //(strlen(friends[i].fname) && strlen(friends[i].lname)&& strlen(friends[i].home)&& strlen(friends[i].cell );
         printf("%s %s %s \n",friends[i].fname,friends[i].lname,friends[i].home,friends[i].cell);  
         flag=1;
      }   
   }
   if(!flag)
      printf("Name not found\n");
   return flag;
}
void readFile(fr*friends ,int* counter, int i,char buffer[],FILE*fpIn)
{
   int j=0,len;
   fr temp;
      
   while (fgets(buffer, 80, fpIn) != NULL) { 
      //printf("%s\n",buffer); //getchar();
      j=0;
      while(buffer[j++] != ' ');    //remember the semi-colon

      temp.fname=malloc(j+1);
      temp.lname=malloc(strlen(buffer)-j+1);

      sscanf(buffer,"%s %s",temp.fname,temp.lname);
      
      fgets(buffer, 20, fpIn);
      len=strlen(buffer);
      j=0;
      while(buffer[j]) {
         if(buffer[j]=='\n')
            buffer[j]='\0';
         ++j;
      }

      temp.home=malloc(len);
      strcpy(temp.home, buffer);

      fgets(buffer, 20, fpIn);
      len=strlen(buffer);

      j=0;
      while(buffer[j]) {
         if(buffer[j]=='\n')
            buffer[j]='\0';
         ++j;
      }

      temp.cell=malloc(len);
      strcpy(temp.cell, buffer); 
      friends[*counter]=temp;
      
      printfile(friends ,*counter);
      (*counter)++; 
   
   }
   fclose(fpIn);
   free(temp.lname);
   free(temp.fname);
}
char printfile(fr*friends ,int counter)
{
   printf("%d. %s, %s, %s  %s\n",counter+1,friends[counter].lname,friends[counter].fname,friends[counter].home,friends[counter].cell);
   return 'a';
}



Mo betta! ;)
Was This Post Helpful? 1
  • +
  • -

#20 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Saving read file data into structure

Posted 19 November 2012 - 04:50 AM

Improved the #4. Onto Delete now.
/*
John Smith
<Home> 8
<Cell> 9
Sue Dyer
<Home> 3
<Cell> 2
Claire Barton
<Home> 1
<Cell> 5
Rob Clearsky
<Home> 4
<Cell> 6
Billy Bestfriend
<Home> 7
<Cell> 10
*/
#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 *fname;
   char *lname;
   char *home;
   char *cell;
}fr;
//Function declarations 
/*
In these functions, if you aren't going to change the counter, you should not have a 
pointer to counter. After you send a pointer to a function, if you subsequently call 
another function (like printfile), send the *counter - because that is NOT a pointer, anymore.
That's the number.
*/

void menu(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 readFile (fr*friends ,int* counter, int i,char buffer[],FILE*read);
char printfile(fr*friends ,int counter);

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]={"phonebook.txt"};
   FILE *fpIn;
   fpIn=fopen(user_entry3,"r+"); //you have to have some data in the phonebook
   //printf("Welcome! Would you like to import a file? (1)Yes or (2) No");
   //scanf("%d",&user_entry1);
   readFile(friends ,counter, i,buffer,fpIn);
   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==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){
           // readFile(friends ,counter,i,buffer,fpIn);
         //}else;
      } 
    
      //if(user_entry==5) {

   
   
   }while(user_entry!=5);
   
}

//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].lname)==0)//Copys over the name entered
      {
         strcpy(friends[i].lname,Delete);
      }
   }
   //Freeing up memory.
   free(friends[i].lname);
   //free
   printf("\nName(s) has been deleted\n");
   return 'a';             
}
//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].fname) && strlen(friends[i].lname)&& strlen(friends[i].home)&& strlen(friends[i].cell ))  {  }
      printfile(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].lname)==0)
      {  /*for alignment, structs are padded. You can't add up struct members and say that will always be the size
         of the struct - use sizeof(nameOfStruct), in the function you create the struct instance 
         Also, different PC's may give a struct a different size because they use different padding. 
         but sizeof(struct) will be accurate on all. */
         
         //(strlen(friends[i].fname) && strlen(friends[i].lname)&& strlen(friends[i].home)&& strlen(friends[i].cell );
         printf("%s %s %s %s\n",friends[i].fname,friends[i].lname,friends[i].home,friends[i].cell);  
         flag=1;
      }   
   }
   if(!flag)
      printf("Name not found\n");
   return flag;
}
void readFile(fr*friends ,int* counter, int i,char buffer[],FILE*fpIn)
{
   int j=0,len;
   fr temp;
   *counter=0;   
   while (fgets(buffer, 80, fpIn) != NULL) { 
      //printf("%s\n",buffer); //getchar();
      j=0;
      while(buffer[j++] != ' ');    //remember the semi-colon

      temp.fname=malloc(j+1);
      temp.lname=malloc(strlen(buffer));

      sscanf(buffer,"%s %s",temp.fname,temp.lname);
      
      fgets(buffer, 20, fpIn);
      len=strlen(buffer);
      j=0;
      while(buffer[j]) {
         if(buffer[j]=='\n')
            buffer[j]='\0';
         ++j;
      }

      temp.home=malloc(len);
      strcpy(temp.home, buffer);

      fgets(buffer, 20, fpIn);
      len=strlen(buffer);

      j=0;
      while(buffer[j]) {
         if(buffer[j]=='\n')
            buffer[j]='\0';
         ++j;
      }

      temp.cell=malloc(len);
      strcpy(temp.cell, buffer); 
      friends[*counter]=temp;
      
      printfile(friends ,*counter);
      (*counter)++; 
   
   }
   fclose(fpIn);
   free(temp.lname);
   free(temp.fname);
}
char printfile(fr*friends ,int counter)
{
   printf("%d. %-10s\t %-10s\t %-8s  %-8s\n",counter+1,friends[counter].lname,friends[counter].fname,friends[counter].home,friends[counter].cell);
   return 'a';
}



All records show OK, as they load, and in #4.

This post has been edited by Adak: 19 November 2012 - 04:52 AM

Was This Post Helpful? 1
  • +
  • -

#21 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Saving read file data into structure

Posted 19 November 2012 - 05:09 AM

This is the fixed up Delete function, for the above version:

//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'};
   int j=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].lname)==0)//Copys over the name entered
      {
         for(j=i;j<*counter-1;j++)
            friends[j] = friends[j+1];
      }
   }
   
   //Freeing up memory.
   free(friends[j].lname);
   free(friends[j].fname);
   free(friends[j].home);
   free(friends[j].cell);
   (*counter)--;
   
   printf("\n Name(s) has been deleted\n",j);
   return 'a';             
}



Was This Post Helpful? 1
  • +
  • -

#22 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Saving read file data into structure

Posted 19 November 2012 - 05:30 AM

Last change tonight for my version. Changed it to fixed length fields for lname, fname, home, and cell.
Delete, readFile, and showing the array, (#4) is all that works.

/*  the data file is:
John Smith
<Home> 8
<Cell> 9
Sue Dyer
<Home> 3
<Cell> 2
Claire Barton
<Home> 1
<Cell> 5
Rob Clearsky
<Home> 4
<Cell> 6
Billy Bestfriend
<Home> 7
<Cell> 10
*/
#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 *fname;
   char *lname;
   char *home;
   char *cell;
}fr;
//Function declarations 
/*
In these functions, if you aren't going to change the counter, you should not have a 
pointer to counter. After you send a pointer to a function, if you subsequently call 
another function (like printfile), send the *counter - because that is NOT a pointer, anymore.
That's the number, and counter (without a star), is now the pointer.
*/

void menu(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 readFile (fr*friends ,int* counter, int i,char buffer[],FILE*read);
char printfile(fr*friends ,int counter);

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]={"phonebook.txt"};
   FILE *fpIn;
   fpIn=fopen(user_entry3,"r+"); //you have to have some data in the phonebook
   //printf("Welcome! Would you like to import a file? (1)Yes or (2) No");
   //scanf("%d",&user_entry1);
   readFile(friends ,counter, i,buffer,fpIn);
   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==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){
           // readFile(friends ,counter,i,buffer,fpIn);
         //}else;
      } 
   }while(user_entry!=5);
}
//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'};
   int j=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].lname)==0)//Copys over the name entered
      {
         for(j=i;j<*counter-1;j++)
            friends[j] = friends[j+1];  //shift all records down one index
         break; //deletes one record at a time
      }
   }
   //Frees up one records allocated memory from the top index
   free(friends[j].lname);
   free(friends[j].fname);
   free(friends[j].home);
   free(friends[j].cell);
   (*counter)--;
   
   printf("\n Name(s) has been deleted\n",j);
   return 'a';             
}
//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].fname) && strlen(friends[i].lname)&& strlen(friends[i].home)&& strlen(friends[i].cell ))  {  }
      printfile(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].lname)==0)
      {  /*for alignment, structs are padded. You can't add up struct members and say that will always be the size
         of the struct - use sizeof(nameOfStruct), in the function you create the struct instance 
         Also, different PC's may give a struct a different size because they use different padding. 
         but sizeof(struct) will be accurate on all. */
         
         //(strlen(friends[i].fname) && strlen(friends[i].lname)&& strlen(friends[i].home)&& strlen(friends[i].cell );
         printf("%s %s %s %s\n",friends[i].fname,friends[i].lname,friends[i].home,friends[i].cell);  
         flag=1;
      }   
   }
   if(!flag)
      printf("Name not found\n");
   return flag;
}
void readFile(fr*friends ,int* counter, int i,char buffer[],FILE*fpIn)
{
   int j=0,len;
   fr temp;
   *counter=0;   
   while (fgets(buffer, 80, fpIn) != NULL) { 
      //printf("%s\n",buffer); //getchar();
      j=0;
      while(buffer[j++] != ' ');    //remember the semi-colon

      temp.fname=malloc(36); //was j+1
      temp.lname=malloc(36); //strlen(buffer));

      sscanf(buffer,"%s %s",temp.fname,temp.lname);
      
      fgets(buffer, 20, fpIn);
      len=strlen(buffer);
      j=0;
      while(buffer[j]) {
         if(buffer[j]=='\n')
            buffer[j]='\0';
         ++j;
      }

      temp.home=malloc(20); //len);
      strcpy(temp.home, buffer);

      fgets(buffer, 20, fpIn);
      len=strlen(buffer);

      j=0;
      while(buffer[j]) {
         if(buffer[j]=='\n')
            buffer[j]='\0';
         ++j;
      }

      temp.cell=malloc(20); //len);
      strcpy(temp.cell, buffer); 
      friends[*counter]=temp;
      
      printfile(friends ,*counter);
      (*counter)++; 
   }
   fclose(fpIn);
   free(temp.lname);
   free(temp.fname);
}
char printfile(fr*friends ,int counter)
{
   printf("%d. %-15s %-15s %-8s %-8s\n",counter+1,friends[counter].lname,friends[counter].fname,friends[counter].home,friends[counter].cell);
   return 'a';
}




Was This Post Helpful? 1
  • +
  • -

#23 jcmoney1010  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-October 12

Re: Saving read file data into structure

Posted 19 November 2012 - 10:45 AM

View PostAdak, on 19 November 2012 - 05:30 AM, said:

Last change tonight for my version. Changed it to fixed length fields for lname, fname, home, and cell.
Delete, readFile, and showing the array, (#4) is all that works.

/*  the data file is:
John Smith
<Home> 8
<Cell> 9
Sue Dyer
<Home> 3
<Cell> 2
Claire Barton
<Home> 1
<Cell> 5
Rob Clearsky
<Home> 4
<Cell> 6
Billy Bestfriend
<Home> 7
<Cell> 10
*/
#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 *fname;
   char *lname;
   char *home;
   char *cell;
}fr;
//Function declarations 
/*
In these functions, if you aren't going to change the counter, you should not have a 
pointer to counter. After you send a pointer to a function, if you subsequently call 
another function (like printfile), send the *counter - because that is NOT a pointer, anymore.
That's the number, and counter (without a star), is now the pointer.
*/

void menu(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 readFile (fr*friends ,int* counter, int i,char buffer[],FILE*read);
char printfile(fr*friends ,int counter);

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]={"phonebook.txt"};
   FILE *fpIn;
   fpIn=fopen(user_entry3,"r+"); //you have to have some data in the phonebook
   //printf("Welcome! Would you like to import a file? (1)Yes or (2) No");
   //scanf("%d",&user_entry1);
   readFile(friends ,counter, i,buffer,fpIn);
   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==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){
           // readFile(friends ,counter,i,buffer,fpIn);
         //}else;
      } 
   }while(user_entry!=5);
}
//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'};
   int j=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].lname)==0)//Copys over the name entered
      {
         for(j=i;j<*counter-1;j++)
            friends[j] = friends[j+1];  //shift all records down one index
         break; //deletes one record at a time
      }
   }
   //Frees up one records allocated memory from the top index
   free(friends[j].lname);
   free(friends[j].fname);
   free(friends[j].home);
   free(friends[j].cell);
   (*counter)--;
   
   printf("\n Name(s) has been deleted\n",j);
   return 'a';             
}
//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].fname) && strlen(friends[i].lname)&& strlen(friends[i].home)&& strlen(friends[i].cell ))  {  }
      printfile(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].lname)==0)
      {  /*for alignment, structs are padded. You can't add up struct members and say that will always be the size
         of the struct - use sizeof(nameOfStruct), in the function you create the struct instance 
         Also, different PC's may give a struct a different size because they use different padding. 
         but sizeof(struct) will be accurate on all. */
         
         //(strlen(friends[i].fname) && strlen(friends[i].lname)&& strlen(friends[i].home)&& strlen(friends[i].cell );
         printf("%s %s %s %s\n",friends[i].fname,friends[i].lname,friends[i].home,friends[i].cell);  
         flag=1;
      }   
   }
   if(!flag)
      printf("Name not found\n");
   return flag;
}
void readFile(fr*friends ,int* counter, int i,char buffer[],FILE*fpIn)
{
   int j=0,len;
   fr temp;
   *counter=0;   
   while (fgets(buffer, 80, fpIn) != NULL) { 
      //printf("%s\n",buffer); //getchar();
      j=0;
      while(buffer[j++] != ' ');    //remember the semi-colon

      temp.fname=malloc(36); //was j+1
      temp.lname=malloc(36); //strlen(buffer));

      sscanf(buffer,"%s %s",temp.fname,temp.lname);
      
      fgets(buffer, 20, fpIn);
      len=strlen(buffer);
      j=0;
      while(buffer[j]) {
         if(buffer[j]=='\n')
            buffer[j]='\0';
         ++j;
      }

      temp.home=malloc(20); //len);
      strcpy(temp.home, buffer);

      fgets(buffer, 20, fpIn);
      len=strlen(buffer);

      j=0;
      while(buffer[j]) {
         if(buffer[j]=='\n')
            buffer[j]='\0';
         ++j;
      }

      temp.cell=malloc(20); //len);
      strcpy(temp.cell, buffer); 
      friends[*counter]=temp;
      
      printfile(friends ,*counter);
      (*counter)++; 
   }
   fclose(fpIn);
   free(temp.lname);
   free(temp.fname);
}
char printfile(fr*friends ,int counter)
{
   printf("%d. %-15s %-15s %-8s %-8s\n",counter+1,friends[counter].lname,friends[counter].fname,friends[counter].home,friends[counter].cell);
   return 'a';
}





sweet, I just tossed it into the compiler as is, and obviously had some problems(crashing after selecting yes from the "would you like to enter a file" menu), but I didn't really look to see what the issue was yet. After being up all night, it's now time for a 10 hour shift at work, so when I get home tonight I'll play around with it and let you know how it goes. Thanks again!
Was This Post Helpful? 0
  • +
  • -

#24 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Saving read file data into structure

Posted 19 November 2012 - 03:13 PM

In my version, the file "phonebook.txt" is always loaded, without prompt. Seemed crazy to have to select that option, every time. Saves a lot of time while debugging it, as well. Same with the filename - it automatically loads the file "phonebook.txt". You don't need to enter the filename every time.

One thing different is the size of the struct members - they are fixed in length. Every lname and fname are the same size - and every home and cell field, as well. Names get 36 chars, and numbers get 20, period. The reason being that it makes the delete function, add function, and load function, easier to code up. (Note that my version doesn't have an add function yet, however). When you delete a record, it simply copies the records with a higher index, down one index, to overwrite the deleted record, and then frees the malloc'd arrays, in the highest possible index record.

That wouldn't be easy to do unless the record fields from one record to the other, were the same size. The databases that I used to work with, had this feature, as well. You could select the length of any field you wanted in the array, but once selected, each record field with that handle (ex. lname), had it's record field, with that same length.

So every add needs to have that "same size" feature as well. Without it, it will crash the program, and the readFile and Delete function, will no longer work.

To test the code, create the phonebook.txt file (data for it is in the top of the program), and let it run - then start deleting records - very nice. ;)/>/> Multiple deletions have been removed, since deleting one family member, shouldn't cause the entire family (with that same last name), to be deleted

I'll write a writeAll (to file) function for it, and an add function, tonight. There's some left over code that I need to remove as well.

This post has been edited by Adak: 19 November 2012 - 03:14 PM

Was This Post Helpful? 1
  • +
  • -

#25 jcmoney1010  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-October 12

Re: Saving read file data into structure

Posted 19 November 2012 - 10:26 PM

View PostAdak, on 19 November 2012 - 03:13 PM, said:

In my version, the file "phonebook.txt" is always loaded, without prompt. Seemed crazy to have to select that option, every time. Saves a lot of time while debugging it, as well. Same with the filename - it automatically loads the file "phonebook.txt". You don't need to enter the filename every time.

One thing different is the size of the struct members - they are fixed in length. Every lname and fname are the same size - and every home and cell field, as well. Names get 36 chars, and numbers get 20, period. The reason being that it makes the delete function, add function, and load function, easier to code up. (Note that my version doesn't have an add function yet, however). When you delete a record, it simply copies the records with a higher index, down one index, to overwrite the deleted record, and then frees the malloc'd arrays, in the highest possible index record.

That wouldn't be easy to do unless the record fields from one record to the other, were the same size. The databases that I used to work with, had this feature, as well. You could select the length of any field you wanted in the array, but once selected, each record field with that handle (ex. lname), had it's record field, with that same length.

So every add needs to have that "same size" feature as well. Without it, it will crash the program, and the readFile and Delete function, will no longer work.

To test the code, create the phonebook.txt file (data for it is in the top of the program), and let it run - then start deleting records - very nice. ;)/>/>/> Multiple deletions have been removed, since deleting one family member, shouldn't cause the entire family (with that same last name), to be deleted

I'll write a writeAll (to file) function for it, and an add function, tonight. There's some left over code that I need to remove as well.


Have it running, and the delete function does work, but this is the output I get when running the program.
1. Smith John <Home> 8 <Cell> 9
2. x☺> x☺> Sue Dyer <Home> 3
3. 2 <Cell>  Claire Barton
4. 1 <Home> <Cell> 5
5. Clearsky Rob <Home> 4 <Cell> 6
6. x☺> x☺> Billy Bestfriend <Home> 7
7. 10 <Cell> <Cell> 10 <Cell> 10

Phone Book Application
1) Add friend
2) Delete friend
3) Show a friend
4) Show phonebook
5)Exit



I'm thinking a memory leak, but not sure where.
Was This Post Helpful? 0
  • +
  • -

#26 jcmoney1010  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-October 12

Re: Saving read file data into structure

Posted 19 November 2012 - 10:37 PM

nvm the last post lol.
Was This Post Helpful? 0
  • +
  • -

#27 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Saving read file data into structure

Posted 19 November 2012 - 10:41 PM

Yes, bad memory allocation. Got tired, and forgot to allocate for the four strings.

New version, MANY changes!

John Smith
<Home> 8
<Cell> 9
Sue Dyer
<Home> 3
<Cell> 2
Claire Barton
<Home> 1
<Cell> 5
Rob Clearsky
<Home> 4
<Cell> 6
Billy Bestfriend
<Home> 7
<Cell> 10
*/
#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define BUFFSIZE 500
#define MAXNAME 36
#define MAXPHONE 20


//Structure for contacts. These are now pointers.
typedef struct friends_contact{

   char *fname;
   char *lname;
   char *home;
   char *cell;
}fr;
//Function declarations 
/*
In these functions, if you aren't going to change the counter, you should not have a 
pointer to counter. After you send a pointer to a function, if you subsequently call 
another function (like printfile), send the *counter - because that is NOT a pointer, anymore.
That's the number.
*/

void menu(fr*friends ,int* counter);
void addNew(fr *friends, int *counter); 
void print_contact(fr*friends ,int* counter);
char delete_contact(fr*friends ,int* counter);
void show_contact(fr*friends ,int *counter);
void readFile (fr*friends ,int* counter,FILE*read);
void printfile(fr*friends ,int counter);

int main() {
   fr friends[7];
   int i,counter=0;
   
   menu(friends,&counter);
   //printf("counter: %d\n",counter);
   
   for(i=0;i<counter;i++) {
      free(friends[i].fname);
      free(friends[i].lname);
      free(friends[i].home);
      free(friends[i].cell);
   }
   return 0;
}
//Menu function
void menu(fr*friends,int* counter) {
   int choice=0;
   char user_entry3[50]={"phonebook.txt"};
   FILE *fpIn;
   fpIn=fopen(user_entry3,"r+"); //you have to have some data in the phonebook
   //printf("Welcome! Would you like to import a file? (1)Yes or (2) No");
   //scanf("%d",&user_entry1);
   //useless without the contact data, so no need to ask.
   readFile(friends,counter,fpIn);
   do{
      printf("\nPhone Book Application\n");
      printf("1) Add friend\n2) Delete friend\n3) Show a friend\n4) Show phonebook\n5)Exit\n");   
      printf("\n\nThere are %d records\n",*counter);
      scanf("%d", &choice);
   
      switch(choice) {
         case 1:
            addNew(friends,counter);
            break;
         case 2:
            delete_contact(friends,counter);
            break; 
         case 3:
            show_contact(friends,counter);
            break;
         case 4:                 
            print_contact(friends,counter);
            break;
         case 5:
            printf("\nGoodbye\n");
            break;
         default:
            printf("Invalid choice, please re-enter\n");
      }
   }while(choice != 5);
   
}
//add a new contact
void addNew(fr *friends, int *counter) {
   int i = *counter,ch=0;
   int len=0;
   fr temp;               //printf("counter: 1.%d  2.%d \n",*counter,counter);    return;
   
   temp.fname=malloc(MAXNAME);
   temp.lname=malloc(MAXNAME);
   temp.home=malloc(MAXPHONE);
   temp.cell=malloc(MAXPHONE);

   friends[i].fname=malloc(MAXNAME);
   friends[i].lname=malloc(MAXNAME);
   friends[i].home=malloc(MAXPHONE);
   friends[i].cell=malloc(MAXPHONE);

   //if(!temp.lname||!temp.fname||!temp.home


   char buff[MAXNAME]={""};
   printf("\n\nAdd a new contact: \nFirst Name: ");
   getchar();      //clear the last newline from input stream
   fgets(temp.fname, MAXNAME-1, stdin);
   len=strlen(temp.fname);
   if(temp.fname[len-1]=='\n')
      temp.fname[len-1]='\0';
   
   printf("Last Name: ");
   fgets(temp.lname,MAXNAME-1,stdin);
   len=strlen(temp.lname);
   if(temp.lname[len-1]=='\n')
      temp.lname[len-1]='\0';

   printf("Home phone: ");
   fgets(buff,MAXPHONE-1,stdin);
   len=strlen(buff);
   if(buff[len-1]=='\n')
      buff[len-1]='\0';
   
   strcpy(temp.home, "<home> ");
   strcat(temp.home, buff);

   printf("Cell phone: ");
   fgets(buff, MAXPHONE-1,stdin);
   len=strlen(buff);
   if(buff[len-1]=='\n')
      buff[len-1]='\0';

   strcpy(temp.cell,"<cell> ");
   strcat(temp.cell, buff);
   //friends[i]=temp;
   strcpy(friends[*counter].fname,temp.fname);
   strcpy(friends[*counter].lname,temp.lname);
   strcpy(friends[*counter].home,temp.home);
   strcpy(friends[*counter].cell,temp.cell);

   //printf("temp: %s   %s   %s   %s\n\n",temp.lname,temp.fname,temp.home,temp.cell); 
   printf("%d. %s   %s   %s   %s\n\n",i+1,friends[i].lname,friends[i].fname,friends[i].home,friends[i].cell); 
   printf("\nAdd this record [y/n]? ");
   ch = getchar();
   getchar();   //clear input stream
   if(ch=='y')
      (*counter)++;
   printf("counter: %d \n",*counter);
}

//This is used to delete a name out of the book.
char delete_contact(fr*friends ,int* counter)
{
   char name_search[50]={'\0'};
   int i,j=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].lname)==0)//Copys over the name entered
      {
         for(j=i;j<*counter-1;j++)
            friends[j] = friends[j+1];  //shift all records down one index
         break; //deletes one record at a time
      }
   }
   
   //Frees up one records allocated memory from the top index
   free(friends[j].lname);
   free(friends[j].fname);
   free(friends[j].home);
   free(friends[j].cell);
   (*counter)--;
   
   printf("\n Name(s) has been deleted\n",j);
   return 'a';             
}
//This function prints out all the contact information
void print_contact(fr*friends ,int* counter) {
   int i;
   for( i = 0; i < *counter; i++) {
      //if (strlen(friends[i].fname) && strlen(friends[i].lname)&& strlen(friends[i].home)&& strlen(friends[i].cell ))  {  }
      printfile(friends,i);
   }    
}
//Displays the contact in which you are searching for.
void show_contact(fr*friends ,int *counter) 
{  
   char name_search2[50]={'\0'};
   int i=0,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].lname)==0)
      {  /*for alignment, structs are padded. You can't add up struct members and say that will always be the size
         of the struct - use sizeof(nameOfStruct), in the function you create the struct instance 
         Also, different PC's may give a struct a different size because they use different padding. 
         but sizeof(struct) will be accurate on all. */
         
         //(strlen(friends[i].fname) && strlen(friends[i].lname)&& strlen(friends[i].home)&& strlen(friends[i].cell );
         printf("%s %s %s %s\n",friends[i].fname,friends[i].lname,friends[i].home,friends[i].cell);  
         flag=1;
      }   
   }
   if(!flag)
      printf("Name not found\n");
}
void readFile(fr*friends ,int* counter,FILE*fpIn)
{
   char buffer[80];
   int len,i;
   fr temp;
   *counter=0;
   i=0;   
   while (fgets(buffer, 80, fpIn) != NULL) { 
      temp.fname=malloc(36); //was j+1
      temp.lname=malloc(36); //strlen(buffer));

      sscanf(buffer,"%s %s",temp.fname,temp.lname);
      
      fgets(buffer, 20, fpIn);
      len=strlen(buffer);
      if(buffer[len-1]=='\n')
         buffer[len-1]='\0';

      temp.home=malloc(20); //len);
      strcpy(temp.home, buffer);

      fgets(buffer, 20, fpIn);
      len=strlen(buffer);
      if(buffer[len-1]=='\n')
         buffer[len-1]='\0';
      

      temp.cell=malloc(20); //len);
      strcpy(temp.cell, buffer); 
      
      friends[i].fname=malloc(MAXNAME);
      friends[i].lname=malloc(MAXNAME);
      friends[i].home=malloc(MAXPHONE);
      friends[i].cell=malloc(MAXPHONE);
     
      if(!friends[i].fname || !friends[i].lname || !friends[i].home || !friends[i].cell) {
         printf("\nmalloc() failed!\n");
         getchar();
         return;
      }

      strcpy(friends[*counter].fname,temp.fname);
      strcpy(friends[*counter].lname,temp.lname);
      strcpy(friends[*counter].home,temp.home);
      strcpy(friends[*counter].cell,temp.cell);
   
      printfile(friends ,i);
      (*counter)++;
      i++; 
     
   }
   fclose(fpIn);
   free(temp.lname);
   free(temp.fname);
   free(temp.home);
   free(temp.cell);
}
void printfile(fr*friends ,int i)
{
   printf("%d. %-15s %-15s  %-8s  %-8s\n",i+1,friends[i].lname,friends[i].fname,friends[i].home,friends[i].cell);
}




This post has been edited by Adak: 19 November 2012 - 10:43 PM

Was This Post Helpful? 1
  • +
  • -

#28 jcmoney1010  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-October 12

Re: Saving read file data into structure

Posted 19 November 2012 - 10:52 PM

View PostAdak, on 19 November 2012 - 10:41 PM, said:

Yes, bad memory allocation. Got tired, and forgot to allocate for the four strings.

New version, MANY changes!

John Smith
<Home> 8
<Cell> 9
Sue Dyer
<Home> 3
<Cell> 2
Claire Barton
<Home> 1
<Cell> 5
Rob Clearsky
<Home> 4
<Cell> 6
Billy Bestfriend
<Home> 7
<Cell> 10
*/
#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define BUFFSIZE 500
#define MAXNAME 36
#define MAXPHONE 20


//Structure for contacts. These are now pointers.
typedef struct friends_contact{

   char *fname;
   char *lname;
   char *home;
   char *cell;
}fr;
//Function declarations 
/*
In these functions, if you aren't going to change the counter, you should not have a 
pointer to counter. After you send a pointer to a function, if you subsequently call 
another function (like printfile), send the *counter - because that is NOT a pointer, anymore.
That's the number.
*/

void menu(fr*friends ,int* counter);
void addNew(fr *friends, int *counter); 
void print_contact(fr*friends ,int* counter);
char delete_contact(fr*friends ,int* counter);
void show_contact(fr*friends ,int *counter);
void readFile (fr*friends ,int* counter,FILE*read);
void printfile(fr*friends ,int counter);

int main() {
   fr friends[7];
   int i,counter=0;
   
   menu(friends,&counter);
   //printf("counter: %d\n",counter);
   
   for(i=0;i<counter;i++) {
      free(friends[i].fname);
      free(friends[i].lname);
      free(friends[i].home);
      free(friends[i].cell);
   }
   return 0;
}
//Menu function
void menu(fr*friends,int* counter) {
   int choice=0;
   char user_entry3[50]={"phonebook.txt"};
   FILE *fpIn;
   fpIn=fopen(user_entry3,"r+"); //you have to have some data in the phonebook
   //printf("Welcome! Would you like to import a file? (1)Yes or (2) No");
   //scanf("%d",&user_entry1);
   //useless without the contact data, so no need to ask.
   readFile(friends,counter,fpIn);
   do{
      printf("\nPhone Book Application\n");
      printf("1) Add friend\n2) Delete friend\n3) Show a friend\n4) Show phonebook\n5)Exit\n");   
      printf("\n\nThere are %d records\n",*counter);
      scanf("%d", &choice);
   
      switch(choice) {
         case 1:
            addNew(friends,counter);
            break;
         case 2:
            delete_contact(friends,counter);
            break; 
         case 3:
            show_contact(friends,counter);
            break;
         case 4:                 
            print_contact(friends,counter);
            break;
         case 5:
            printf("\nGoodbye\n");
            break;
         default:
            printf("Invalid choice, please re-enter\n");
      }
   }while(choice != 5);
   
}
//add a new contact
void addNew(fr *friends, int *counter) {
   int i = *counter,ch=0;
   int len=0;
   fr temp;               //printf("counter: 1.%d  2.%d \n",*counter,counter);    return;
   
   temp.fname=malloc(MAXNAME);
   temp.lname=malloc(MAXNAME);
   temp.home=malloc(MAXPHONE);
   temp.cell=malloc(MAXPHONE);

   friends[i].fname=malloc(MAXNAME);
   friends[i].lname=malloc(MAXNAME);
   friends[i].home=malloc(MAXPHONE);
   friends[i].cell=malloc(MAXPHONE);

   //if(!temp.lname||!temp.fname||!temp.home


   char buff[MAXNAME]={""};
   printf("\n\nAdd a new contact: \nFirst Name: ");
   getchar();      //clear the last newline from input stream
   fgets(temp.fname, MAXNAME-1, stdin);
   len=strlen(temp.fname);
   if(temp.fname[len-1]=='\n')
      temp.fname[len-1]='\0';
   
   printf("Last Name: ");
   fgets(temp.lname,MAXNAME-1,stdin);
   len=strlen(temp.lname);
   if(temp.lname[len-1]=='\n')
      temp.lname[len-1]='\0';

   printf("Home phone: ");
   fgets(buff,MAXPHONE-1,stdin);
   len=strlen(buff);
   if(buff[len-1]=='\n')
      buff[len-1]='\0';
   
   strcpy(temp.home, "<home> ");
   strcat(temp.home, buff);

   printf("Cell phone: ");
   fgets(buff, MAXPHONE-1,stdin);
   len=strlen(buff);
   if(buff[len-1]=='\n')
      buff[len-1]='\0';

   strcpy(temp.cell,"<cell> ");
   strcat(temp.cell, buff);
   //friends[i]=temp;
   strcpy(friends[*counter].fname,temp.fname);
   strcpy(friends[*counter].lname,temp.lname);
   strcpy(friends[*counter].home,temp.home);
   strcpy(friends[*counter].cell,temp.cell);

   //printf("temp: %s   %s   %s   %s\n\n",temp.lname,temp.fname,temp.home,temp.cell); 
   printf("%d. %s   %s   %s   %s\n\n",i+1,friends[i].lname,friends[i].fname,friends[i].home,friends[i].cell); 
   printf("\nAdd this record [y/n]? ");
   ch = getchar();
   getchar();   //clear input stream
   if(ch=='y')
      (*counter)++;
   printf("counter: %d \n",*counter);
}

//This is used to delete a name out of the book.
char delete_contact(fr*friends ,int* counter)
{
   char name_search[50]={'\0'};
   int i,j=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].lname)==0)//Copys over the name entered
      {
         for(j=i;j<*counter-1;j++)
            friends[j] = friends[j+1];  //shift all records down one index
         break; //deletes one record at a time
      }
   }
   
   //Frees up one records allocated memory from the top index
   free(friends[j].lname);
   free(friends[j].fname);
   free(friends[j].home);
   free(friends[j].cell);
   (*counter)--;
   
   printf("\n Name(s) has been deleted\n",j);
   return 'a';             
}
//This function prints out all the contact information
void print_contact(fr*friends ,int* counter) {
   int i;
   for( i = 0; i < *counter; i++) {
      //if (strlen(friends[i].fname) && strlen(friends[i].lname)&& strlen(friends[i].home)&& strlen(friends[i].cell ))  {  }
      printfile(friends,i);
   }    
}
//Displays the contact in which you are searching for.
void show_contact(fr*friends ,int *counter) 
{  
   char name_search2[50]={'\0'};
   int i=0,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].lname)==0)
      {  /*for alignment, structs are padded. You can't add up struct members and say that will always be the size
         of the struct - use sizeof(nameOfStruct), in the function you create the struct instance 
         Also, different PC's may give a struct a different size because they use different padding. 
         but sizeof(struct) will be accurate on all. */
         
         //(strlen(friends[i].fname) && strlen(friends[i].lname)&& strlen(friends[i].home)&& strlen(friends[i].cell );
         printf("%s %s %s %s\n",friends[i].fname,friends[i].lname,friends[i].home,friends[i].cell);  
         flag=1;
      }   
   }
   if(!flag)
      printf("Name not found\n");
}
void readFile(fr*friends ,int* counter,FILE*fpIn)
{
   char buffer[80];
   int len,i;
   fr temp;
   *counter=0;
   i=0;   
   while (fgets(buffer, 80, fpIn) != NULL) { 
      temp.fname=malloc(36); //was j+1
      temp.lname=malloc(36); //strlen(buffer));

      sscanf(buffer,"%s %s",temp.fname,temp.lname);
      
      fgets(buffer, 20, fpIn);
      len=strlen(buffer);
      if(buffer[len-1]=='\n')
         buffer[len-1]='\0';

      temp.home=malloc(20); //len);
      strcpy(temp.home, buffer);

      fgets(buffer, 20, fpIn);
      len=strlen(buffer);
      if(buffer[len-1]=='\n')
         buffer[len-1]='\0';
      

      temp.cell=malloc(20); //len);
      strcpy(temp.cell, buffer); 
      
      friends[i].fname=malloc(MAXNAME);
      friends[i].lname=malloc(MAXNAME);
      friends[i].home=malloc(MAXPHONE);
      friends[i].cell=malloc(MAXPHONE);
     
      if(!friends[i].fname || !friends[i].lname || !friends[i].home || !friends[i].cell) {
         printf("\nmalloc() failed!\n");
         getchar();
         return;
      }

      strcpy(friends[*counter].fname,temp.fname);
      strcpy(friends[*counter].lname,temp.lname);
      strcpy(friends[*counter].home,temp.home);
      strcpy(friends[*counter].cell,temp.cell);
   
      printfile(friends ,i);
      (*counter)++;
      i++; 
     
   }
   fclose(fpIn);
   free(temp.lname);
   free(temp.fname);
   free(temp.home);
   free(temp.cell);
}
void printfile(fr*friends ,int i)
{
   printf("%d. %-15s %-15s  %-8s  %-8s\n",i+1,friends[i].lname,friends[i].fname,friends[i].home,friends[i].cell);
}





Everything works except for the Delete function. Well it works, but it outputs weird results when deleting names. Sometimes it gives weird characters, other times it will delete more then one name, and other times it might delete a name other then the one you selected. Regardless you have been extremely generous and I can't thank you enough.
Was This Post Helpful? 0
  • +
  • -

#29 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Saving read file data into structure

Posted 19 November 2012 - 11:03 PM

Let me double check it because I deleted every name in the array, and then added 3 names back into the array, and it did fine.

One minute here.

Well, that's VERY unkind of it! *&!~%$)@*%$@

Update:
char delete_contact(fr*friends ,int* counter)
{
   char name_search[50]={'\0'};
   int i,j=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].lname)==0)//Copys over the name entered
      {
         for(j=i;j<*counter-1;j++) {
            friends[j] = friends[j+1];  //shift all records down one index
            strcpy(friends[j].fname,friends[j+1].fname);               
            strcpy(friends[j].lname,friends[j+1].lname);
            strcpy(friends[j].home,friends[j+1].home);
            strcpy(friends[j].cell,friends[j+1].cell);
         }
         break; //deletes one record at a time
      }
   }
   
   //Frees up one records allocated memory from the top index
   free(friends[j].lname);
   free(friends[j].fname);
   free(friends[j].home);
   free(friends[j].cell);
   (*counter)--;
   
   printf("\n Name(s) has been deleted\n",j);
   return 'a';             
}




The "inner" arrays we malloc, have to be copied over, one at a time.

This post has been edited by Adak: 19 November 2012 - 11:21 PM

Was This Post Helpful? 1
  • +
  • -

#30 jcmoney1010  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-October 12

Re: Saving read file data into structure

Posted 19 November 2012 - 11:36 PM

I've melded what you've given me to what I had originally and am in the process of trying to tweek it so it works properly, while it still has some work to be done, you have been an unbelievably helpful resource. I can't begin to thank you enough. Hopefully I won't get stuck anymore and I can have this completely finished by the time it's due (9 am ). You rock!!
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3