3

When I read information from a file into my phonebook program and try to view it via the contacts list, the list is blank the first time you try and view it, but if you choose to check it again, the content from the file is there. Even weirder, when you add a name via the program, the name shows up once along with the file data, but then if you check it again, the new name is gone. If you delete a name, it deletes it once, but if you check the contacts list again, the name comes back. I'm at a loss. Here is my Read Function and my Print function.

void file2(fr*friends ,int* counter, int i,char buffer[],FILE*read,char user_entry3[])
{
    int len;
    fr temp;
    *counter=0;
    i=0; 
    fseek(read, 0, SEEK_SET);  
    while (fgets(buffer, 80, read) != NULL) { 
        temp.First_Name=malloc(36); //was j+1
        temp.Last_Name=malloc(36); //strlen(buffer));

        sscanf(buffer,"%s %s",temp.First_Name,temp.Last_Name);

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

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

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


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

        friends[i].First_Name=malloc(MAXNAME);
        friends[i].Last_Name=malloc(MAXNAME);
        friends[i].home=malloc(MAXPHONE);
        friends[i].cell=malloc(MAXPHONE);

        if(!friends[i].First_Name || !friends[i].Last_Name || !friends[i].home || !friends[i].cell) {
            printf("\nmalloc() failed!\n");
            getchar();
            return;
        }

        strcpy(friends[*counter].First_Name,temp.First_Name);
        strcpy(friends[*counter].Last_Name,temp.Last_Name);
        strcpy(friends[*counter].home,temp.home);
        strcpy(friends[*counter].cell,temp.cell);


        (*counter)++;
        i++; 

    }
    //fclose(read);
    free(temp.Last_Name);
    free(temp.First_Name);
    free(temp.home);
    free(temp.cell);
}


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);

}

It's weird because it's like the program works perfectly once, but then not again.

Jcmoney1010
  • 912
  • 7
  • 18
  • 41
  • 3
    that's not enough of your code -- where's `main()`? Also, your print routine is calling your load routine, _after_ it has printed your list -- that can't be right. – Greg A. Woods Nov 20 '12 at 09:04

1 Answers1

0

It's weird because it's like the program works perfectly once, but then not again.

I'm afraid it really is only as if it worked:

  • friends is preallocated somewhere? For you just keep reading input from file and incrementing i till the end of file (or error) - that's likely to overflow sooner or later;

  • you read 80 characters, and then try to put it to 2 times 36 characters - that can easily overflow again. Not mentioning reading those 80 into a buffer that is passed as argument through several functions - why not to use local variable? Is the magic number 36 smaller, larger or equal to MAXNAME? Why?

  • malloc() temp on every iteration of the loop but free() it only once after it finishes - leaking memory ~120B/entry;

peterph
  • 980
  • 6
  • 11