1

我一直在尝试在二进制文件和链表之间进行读写。有人可以解释我做错了什么吗?

节省:

currentContact = firstContact;


while( currentContact != NULL )
{
fwrite (currentContact->firstName, sizeof currentContact->firstName, 1, myFile);
fwrite (currentContact->surname, sizeof currentContact->surname, 1, myFile);
fwrite (&currentContact->age, sizeof (int), 1, myFile);
fwrite (currentContact->telephone, sizeof currentContact->telephone, 1, myFile);
currentContact = currentContact->next;
}

加载:

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

编辑:

应用一些更改后,通过循环再次解析文件的第二次解析仍然存在错误......

                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++;                    
            }
4

2 回答 2

1

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:

  • The code should probably be allocating memory for each new contact (since what appears to be the first allocation is only for a single element).
  • The next pointer should be initialized to NULL at some point (realloc does not zero out memory).
  • The linking in of new elements appears to have some issues. For example, if the new element is the new first element, then the head pointer firstContact should be updated. Also, if the new element is greater than all existing ones, it will not be added to the list.
  • The code should check the result of fread (for error conditions).

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.

于 2013-01-04T20:27:33.887 回答
0

我认为你的问题已经在

fwrite (currentContact->firstName

您需要做的是编写整个结构而不是编写每个成员

fwrite (currentContact, sizeof(type of currentContact),..// 或任何你的结构体被调用。

于 2013-01-04T20:35:14.820 回答