0

这是我在这个网站上的第一个问题。

我一直在为我的大学作业制作一个 C 程序。游戏而已。我使用 calloc 为结构数组动态分配内存。然后我从一个文件中读取(它已经具有由 fwrite 写入的相同结构)并将信息存储在我从 calloc 创建的这些结构中。然后我处理信息并写回文件。我面临的问题是,当我使用“wb”覆盖同一个文件并使用 fwrite 写回处理过的结构时,它只写入数组的第一个结构,而其他结构则不会被写入并从内存中丢失。我敢肯定,在使用 fwrite 之前,内存中的所有信息都是完整的。所以,我在处理过程中没有做错任何事情。但是 fwrite 只写第一个结构。任何帮助将不胜感激。这是代码的一部分:

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

}
4

1 回答 1

3

fwrite不保证它会写入您传递给它的所有数据,因此您必须循环调用它。但是,我不认为这可能是您的问题的原因。

我认为您的问题是由于您滥用指针算术这一事实引起的。如果high是 astruct scores*并且你想要i它指向的数组的 -th 元素,你可以

high + i

不是,正如您在整个代码中看到的那样,

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

这样,您正在处理非常不同的内存位置(并且很可能调用 UB)。

于 2013-01-01T18:06:04.143 回答