比较fgetc()的返回值和一个随机数字

时间:2018-09-16 01:03:34

标签: c random char

我试图读取文件,直到我遇到1到5之间的随机整数,然后将该整数旁边的单词读入数组。在尝试将随机int与fgetc()的返回值进行比较之前,代码运行良好(它将char读入数组直到行尾)。现在,当我在调试器中时,我看到我要查找的整数的fgetc()返回值是ASCII十进制值,而不是实际的整数。因此,当我将其与随机整数进行比较时,永远找不到它。我不明白该如何正确处理。这是代码,对于法语中的注释,抱歉,它们不相关。

int lectureMot(FILE *listePtr, char motsecret[])
{
    int caracLu;
    int nbLettres = 0;
    int numRand;
    numRand = rand()%((6-1)+1);

    /*Ouverture du fichier texte de la liste*/
    listePtr = fopen("Liste.txt","r");

    /*Verification de l'ouverture du fichier*/
    if (listePtr == NULL)
    {
        printf("Erreur a l'ouverture de la liste");
        return(EXIT_FAILURE);
    }
    else
    {
        /*Lire jusqu'au nombre random*/
        while((caracLu = fgetc(listePtr)) != numRand);

        /*Lire le mot et le mettre dans le tableau*/
        while((caracLu = fgetc(listePtr)) != '\n')
        {
            motsecret[nbLettres] = (char)caracLu;
            nbLettres = ++nbLettres;
        }
    }
    /*Fermeture du fichier liste*/
    fclose(listePtr);
    return nbLettres;
}

1 个答案:

答案 0 :(得分:0)

只需在您的数字后添加“ 0”字符即可:

更改

const fooConstructor: PossiblyAbstractConstructor<Foo> = Foo;
const barConstructor: PossiblyAbstractConstructor<Bar> = Bar;

numRand = rand()%((6-1)+1);