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);
}
*/
}
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).