0

これは、このWebサイトでの私の最初の質問です。

私は大学の課題のために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は、次のようにします。struct scores*i

high + i

コード全体に見られるように、そうではありません。

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

このようにして、非常に異なるメモリ位置に対処します(そしておそらくUBを呼び出します)。

于 2013-01-01T18:06:04.143 に答える