Saving read file data into structure

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

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

#31 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:44 PM

Thanks for the kind words, JC. Interesting working on it. I haven't done much with structs that use pointers to arrays.
Was This Post Helpful? 1
  • +
  • -

#32 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 20 November 2012 - 12:30 AM

I added / changed this part of the delete function, to make it more robust - mostly stops counter from becoming less than 0.

   j=*counter-1;   //new
   //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)--;
   if(*counter<0)  //new
      *counter=0;  //new

   printf("\n Name(s) has been deleted\n",j);
   return 'a';             
}



Was This Post Helpful? 1
  • +
  • -

#33 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 20 November 2012 - 12:34 AM

View PostAdak, on 19 November 2012 - 11:44 PM, said:

Thanks for the kind words, JC. Interesting working on it. I haven't done much with structs that use pointers to arrays.


One last question and I won't bother you again :)/>/>. Obviously since I'm turning this in for a grade, I had to keep a lot of what I did rather then just turning in what you did, I basically kept the readFile function as is, but deleted the call to printfile and the print function. I'm using my original Print_Contacts function, and my issue is , I can get the contacts the show, and I can search for a name via the last name, but whenever I try and add a new contact, it just gets deleted and the file contacts print out again.
void print_contact(fr*friends ,int* counter, int i,char buffer[],char user_entry3[50],FILE*read) {
file2(friends ,counter,i,buffer,read,user_entry3);//function call!!
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);
            
           
            
    }
 

}


when I change the position of the function call to here:
void print_contact(fr*friends ,int* counter, int i,char buffer[],char user_entry3[50],FILE*read) {

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,buffer,read,user_entry3);//Function calll!!

}


It my contacts list will be blank the first time I check it, but when I check it again the names will be listed from the file. From here I can add a new name, and it appears just once in the contact list before being deleted again. I'm so confused! Oh ya, and this only works when I comment out the fclose statement, otherwise it works once and then is blank again.

This post has been edited by jcmoney1010: 20 November 2012 - 12:38 AM

Was This Post Helpful? 0
  • +
  • -

#34 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 20 November 2012 - 01:08 AM

I can't say too much about it since I haven't seen any of your changes in the code since post #1. Sounds like counter is not being incremented when you add a new record? Or you have 5 records already in the struct array, and that's the size of the records that it was created to hold.
Was This Post Helpful? 1
  • +
  • -

#35 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 20 November 2012 - 01:17 AM

View PostAdak, on 20 November 2012 - 01:08 AM, said:

I can't say too much about it since I haven't seen any of your changes in the code since post #1. Sounds like counter is not being incremented when you add a new record? Or you have 5 records already in the struct array, and that's the size of the records that it was created to hold.


That's what's weird, because outside of what I mentioned in the last post, no changes have been made. the program works once each loop through and then it restarts itself and no changes you made ( deletion or addition) save. Hmm and the plot thickens!
Was This Post Helpful? 0
  • +
  • -

#36 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 20 November 2012 - 01:26 AM

I'd say so! Go get 'em detective!

Worse comes to worse, put the other function back in. ;)
Was This Post Helpful? 1
  • +
  • -

#37 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 20 November 2012 - 01:54 AM

View PostAdak, on 20 November 2012 - 01:26 AM, said:

I'd say so! Go get 'em detective!

Worse comes to worse, put the other function back in. ;)/>


I fixed it! the program is fully functional and working, and I couldn't have done it without you. I hope you win the lottery tomorrow, because you deserve it. You really saved me on this one, I will sing your praises throughout the land lol. Have a good one Sir!
Was This Post Helpful? 0
  • +
  • -

#38 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 20 November 2012 - 02:05 AM

You're quite welcome, and I'll give the lottery a go! ;)/>

Job well done.

This post has been edited by Adak: 20 November 2012 - 02:06 AM

Was This Post Helpful? 0
  • +
  • -

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