Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9086499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:30:16+00:00 2026-06-16T21:30:16+00:00

I have been trying for to write and read between a binary file and

  • 0

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 (&currentContact->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++;                    
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:30:17+00:00Added an answer on June 16, 2026 at 9:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to write some code in Scala to read a file
i have been trying to write a program in python to read two text
I have been trying to write a custom .screenrc file TEST as follows startup_message
I have been trying to write and read files directly from the BlobStore, but
I have been trying to write a script where I can post data to
I have been trying to write and read files through P/Invoke via my ASP.NET
I have been trying to write a bare-bones ping scanner using Perl for internal
I have been trying to write an image on a layer using Quartz but
I have been trying to write my own diff3 wrap script for SVN and
I have been trying to write a regex that will remove whitespace following a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.