【问题标题】:Program crashing and returning 255程序崩溃并返回 255
【发布时间】:2013-10-19 22:18:51
【问题描述】:

我正在编写一个“Rock, Papers and Scissors”程序。当玩家输掉或赢得比赛时,它会跟踪玩家的得分并将其保存到磁盘。但是不知道为什么,程序运行起来,没有构建错误或警告,但是每当分数发生变化时,程序就会崩溃并返回255。

// Pedra, papel e tesoura

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

void showRes(int cho1);

int scorei;
char score[128];

int main()
{
    int ctemp,done;
    char cho1;
    FILE *sco;

    sco = fopen("score.txt","r");
    if(sco == NULL)
        scorei = 0;
    else
    {
        fread(score,128,1,sco);
        scorei = atoi(score);
    }
    fclose(sco);
    sco = fopen("score.txt","w");

    srand((unsigned)time(NULL));
    done = 0;
    printf("Pedra, Papel e Tesoura\n");
    do
    {
        printf("\n Score: %08d\n",scorei);
        printf("\nDigite '1' para 'Pedra', '2' para 'Papel', '3' para 'Tesoura' ou 'Q' para sair: ");
        cho1 = toupper(getchar());
        fflush(stdin);
        ctemp = cho1;
        switch(ctemp)
        {
            case 49:
            case 50:
            case 51:
                showRes(ctemp);
                break;
            case 81:
                done = 1;
                break;
            default:
                printf("Escolha Inv%clida!\n",160);
                break;
        }
        fwrite(scorei,sizeof(int),1,sco);
        printf("\nsalvo com sucesso\n\n");
    }
    while(!done);
    fclose(sco);

    return(0);
}

void showRes(int cho)
{
    int ia;

    ia = rand() % 3 + 1;

    if(cho == 49)
    {
        printf("Voc%c escolheu Pedra!\n",136);
        switch(ia)
        {
            case 1:
                printf("O Computador escolheu Pedra!\n");
                printf("Empate!");
                break;
            case 2:
                printf("O Computador escolheu Papel!\n");
                printf("Voc%c perdeu!",136);
                scorei -= 100;
                break;
            case 3:
                printf("O Computador escolheu Tesoura!\n");
                printf("Voc%c venceu!\n",136);
                scorei += 100;
                break;
            default:
                break;
        }
    }
    if(cho == 50)
    {
        printf("Voc%c escolheu Papel!\n",136);
        switch(ia)
        {
            case 1:
                printf("O Computador escolheu Pedra!\n");
                printf("Voc%c venceu!\n",136);
                scorei += 100;
                break;
            case 2:
                printf("O Computador escolheu Papel!\n");
                printf("Empate!\n");
                break;
            case 3:
                printf("O Computador escolheu Tesoura!\n");
                printf("Voc%c perdeu!\n",136);
                scorei -= 100;
                break;
            default:
                break;
        }
    }
    if(cho == 51)
    {
        printf("Voc%c escolheu Tesoura!\n",136);
        switch(ia)
        {
            case 1:
                printf("O Computador escolheu pedra!\n");
                printf("Voc%c perdeu!\n",136);
                scorei -= 100;
                break;
            case 2:
                printf("O Computador escolheu Papel!\n");
                printf("Voc%c venceu!\n",136);
                scorei += 100;
                break;
            case 3:
                printf("O Computador escolheu Tesoura!\n");
                printf("Empate!\n");
            default:
                break;
        }
    }
}

【问题讨论】:

  • 这一行没有意义:fwrite(scorei,sizeof(int),1,sco);你不应该写一个文件吗?您正在写入一个整数。
  • 请学会调试足以识别故障线。除非你学会调试,否则你不能期望取得进步。
  • 这不就是我们将信息写入文件的方式吗? sco = fopen("score.txt","w");,所以fwrite(scorei,sizeof(int),1,sco); 正在将变量“scorei”写入“score.txt”,不是吗? @零.零.七
  • 不,你需要&scorei,你要写入的数据的地址。您是否忽略了编译器的警告?他们试图告诉你这些事情。
  • 您应该拿起一本书或阅读在线教程。在谷歌搜索“fwrite + C”

标签: c crash


【解决方案1】:

不一定是崩溃的原因,但您正在读取文件,就好像它包含文本一样,但正在向它写入二进制值。此外,连续的值被添加到文件中,但您只读出了 1 个。

【讨论】:

    猜你喜欢
    • 2020-06-07
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2023-01-08
    • 1970-01-01
    • 2013-07-10
    • 2021-05-30
    • 1970-01-01
    相关资源
    最近更新 更多