Reputation: 40
I have been trying for to write and read between a binary file and a linked list. Can someone explain what I'm doing wrong?
Save:
currentContact = firstContact;
while( currentContact != NULL )
{
fwrite (currentContact->firstName, sizeof currentContact->firstName, 1, myFile);
fwrite (currentContact->surname, sizeof currentContact->surname, 1, myFile);
fwrite (¤tContact->age, sizeof (int), 1, myFile);
fwrite (currentContact->telephone, sizeof currentContact->telephone, 1, myFile);
currentContact = currentContact->next;
}
Load:
fread( &numContacts, sizeof( int ), 1, myFile );
newContact = realloc( newContact, sizeof( struct Contact ) * 1 );
countFile = 1;
while (fread(newContact, sizeof( struct Contact ), 1, myFile))
{
fread(newContact->firstName, sizeof newContact->firstName, 1, myFile);
fread(newContact->surname, sizeof newContact->surname, 1, myFile);
fread((&newContact->age), sizeof (int), 1, myFile);
fread(newContact->telephone, sizeof newContact->telephone, 1, myFile);
if (countFile == 1)
{
firstContact = newContact;
newContact = NULL;
}
else
{
currentContact = firstContact;
count = 0;
while( count != countFile )
{
if( strcmp( newContact->surname, currentContact->surname ) < 0 )
{
newContact->next = currentContact->next;
currentContact->next = newContact;
}
currentContact = currentContact->next;
}
newContact = NULL;
}
countFile++;
}
fclose( myFile );
EDIT:
After applying some changes, there is still an error on the second parse through the loop where it goes to fread the file again...
newContact = realloc( newContact, sizeof( struct Contact ) * numContacts );
countFile = 1;
while (countFile != numContacts + 1)
{
fread(newContact, sizeof (struct Contact), 1, myFile);
if (countFile == 1)
{
firstContact = newContact;
newContact = NULL;
}
else
{
currentContact = firstContact;
count = 0;
while( count != countFile )
{
if( strcmp( newContact->surname, currentContact->surname ) < 0 )
{
newContact->next = currentContact->next;
currentContact->next = newContact;
}
currentContact = currentContact->next;
}
newContact = NULL;
}
countFile++;
}
Upvotes: 1
Views: 1306
Reputation: 36102
I think your problem is already at
fwrite (currentContact->firstName
what you need to do is write the whole structure not writing each member
fwrite (currentContact, sizeof(type of currentContact),..
// or whatever your struct is called.
Upvotes: 0
Reputation: 41252
It looks like one problem is the following statement:
while (fread(newContact, sizeof( struct Contact ), 1, myFile))
The above statement is attempting to read something into the memory pointed to by newContact
. Based on the way the data is written, this might not be correct. And based on the fact that the next statements inside the loop read the individual members, then it is not correct. So that fread
inside the while loop should probably be eliminated.
Some other potential issues:
next
pointer should be initialized to NULL at some point (realloc does not zero out memory).firstContact
should be updated. Also, if the new element is greater than all existing ones, it will not be added to the list.After OP Edits The allocation for the entire list in one piece is a reasonable idea. However, it is necessary to update newContact to point to the correct piece of memory at each iteration in the loop. You could keep a separate pointer variable for maintaining that information. In addition, the change to read the entire contact in one piece needs to be reflected in the writing code too. As is, the amount of data written and read is unlikely to be the same (e.g., the structure has a next member variable that takes up space and is not reflected in the written data.
Upvotes: 1