dimanche 16 septembre 2018

comparing the return value of fgetc() to a random digit

I am trying to read a file until i hit a random int between 1 and 5 and then read the word next to that int into an array. Before trying to compare a random int with the return value of fgetc(), the code was working well (It read chars into an array until an end of line). Now, when i am in the debugger, i see that the return value of fgetc() for the integers i am looking for is the ASCII decimal value, not the actual integer. So when i compare with the random int, it is never found. I don't understand how to get this right. Here is the code, sorry for the comments being in french, they are not relevant.

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;
}




Aucun commentaire:

Enregistrer un commentaire