将二进制文件读取到链接列表

时间:2013-01-04 20:20:48

标签: c list binary

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

保存:

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

2 个答案:

答案 0 :(得分:1)

看起来一个问题是以下声明:

while (fread(newContact, sizeof( struct Contact ), 1, myFile))

上述语句试图将某些内容读入newContact指向的内存中。根据数据的写入方式,可能不正确。并且基于循环内的下一个语句读取各个成员的事实,那么它是不正确的。因此,应该消除while循环中的fread

其他一些潜在问题:

  • 代码可能应该是为每个新联系人分配内存(因为看起来第一次分配只针对单个元素)。
  • next指针应该在某个时刻初始化为NULL(realloc不会将内存清零)。
  • 新元素的链接似乎存在一些问题。例如,如果新元素是新的第一个元素,则应更新头指针firstContact。此外,如果新元素大于所有现有元素,则不会将其添加到列表中。
  • 代码应该检查fread的结果(对于错误条件)。

OP编辑后整个列表的分配是一个合理的想法。但是,有必要更新newContact以指向循环中每次迭代时的正确内存块。您可以保留一个单独的指针变量来维护该信息。另外,读取整个联系人的变化也需要反映在编写代码中。因此,写入和读取的数据量不太可能相同(例如,结构具有占用空间的下一个成员变量,并且不会反映在书面数据中。

答案 1 :(得分:0)

我认为您的问题已经存在

fwrite (currentContact->firstName

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

fwrite (currentContact, sizeof(type of currentContact),.. //或者调用任何ur结构。

相关问题