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 9053311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:23:35+00:00 2026-06-16T13:23:35+00:00

this is my first question on this website. I have been making a C

  • 0

this is my first question on this website.

I have been making a C program for my college assignment. It’s a game. I have used calloc to dynamically allocate memory for an array of structures. I am then reading from a file (which already has the same structures written in it by fwrite) and storing the information in these structures I create from calloc. I then process the information and write back to the file. The problem I’m facing is that when I use “wb” to overwrite the same file and write back the processed structures with fwrite, it only writes the first structure of the array and the others are somehow not written and lost from the memory. I’m certain that before using fwrite, all the information is intact in the memory. So, I’m not doing anything wrong in the processing. But fwrite doesn’t write more than the first structure. Any help will be greatly appreciated. Here is the part of the code :

high = (struct scores *) calloc(HIGHSCOREENTERIES + 1, sizeof(struct scores));
junk = high + (HIGHSCOREENTERIES * sizeof(struct scores));


if((scorefile = fopen(HIGHSCORESFILE, "rb")) != 0)
{
    temp_high = high;
    printf("\n\nStoring in : %p", temp_high);
    fread(temp_high, sizeof(struct scores), 3, scorefile);

    if (temp_high -> score > 0)
    {
        printf("\n\nHigh Scores : ");

        for(i = 0; i < HIGHSCOREENTERIES; i++)
        {
            temp_high = high + (i * sizeof(struct scores));

            if (temp_high -> score > 0)
                printf("\n\n%d. %s had a score of %d.", i + 1, temp_high -> name, temp_high -> score);
        }
    }
    fclose(scorefile);
}

if (!custom && player.score > 0 && strcmp(player.name, "Cheater") != 0)
{
    temp_high = high;

    for (i = (HIGHSCOREENTERIES - 1); i >= 0; i--)
    {
        temp_high = high + (i * sizeof(struct scores));
        printf("\n\nAddress is : %p", temp_high);

        if (player.score > temp_high -> score)
        {
            printf("\n\nMoving old information to : %p", (temp_high + sizeof(struct scores)));
            (temp_high + sizeof(struct scores)) -> score = temp_high -> score;
            strcpy((temp_high + sizeof(struct scores)) -> name, temp_high -> name);

            junk -> score = temp_high -> score;
            strcpy(junk -> name, temp_high -> name);

            printf("\n\nMoving player's score to to : %p", temp_high);
            temp_high -> score = player.score;
            strcpy(temp_high -> name, player.name);
        }
    }

    if (junk -> score != 0)
        printf("\n\n*Congrats! You beat %s's score of %d.*", junk -> name, junk -> score);

    else
        printf("\n\nCongrats! Your name is now in the highscores list!");


    temp_high = high;

    /*For debugging
     for(i = 0; i < HIGHSCOREENTERIES; i++)
     {
     temp_high = high + (i * sizeof(struct scores));

     if (temp_high -> score > 0)
     printf("\n\n%d. %s had a score of %d.", i + 1, temp_high -> name, temp_high -> score);
     }
     */

    temp_high = high;

    if((scorefile = fopen(HIGHSCORESFILE, "wb")) != 0)
    {
        printf("\n\nWriting from : %p", temp_high);
        fwrite(temp_high, sizeof(struct scores), 3, scorefile);
        fclose(scorefile);
    }

    /*For debugging
     for(i = 0; i < HIGHSCOREENTERIES; i++)
     {
     temp_high = high + (i * sizeof(struct scores));

     if (temp_high -> score > 0)
     printf("\n\n%d. %s had a score of %d.", i + 1, temp_high -> name, temp_high -> score);
     }
     */

}
  • 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-16T13:23:36+00:00Added an answer on June 16, 2026 at 1:23 pm

    fwrite doesn’t guarantee that it writes all the data you pass to it, so you have to call it in a loop. However, I don’t think that could be the cause of your problem.

    I think your problems are caused by the fact that you misuse pointer arithmetic. If high is a struct scores* and you want i-th element of the array it points to, you do

    high + i
    

    Not, as is seen throughout your code,

    temp_high = high + (i * sizeof(struct scores));
    

    This way, you’re addressing very different memory locations (and most likely invoking UB).

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

Sidebar

Related Questions

Hey this is my first question on this website. Im creating a webpage for
this is my first question I have searched for any launching activity questions and
This question hasn't been asked yet. Some solutions probably have been proposed though to
I am new to this website; in fact this is my first question. So
Preamble So, this question has already been answered, but as it was my first
this is my first question in here, and I would like to ask if
This is my first question on Stack Overflow, so I hope that I'm clear
This is my first question, so please be gentle. I'm trying out Apache Camel
this is my first question to SO so i'll try not to disgrace myself.
this is my first question here so be gentle ! recently i try to

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.