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.