简体   繁体   中英

I am unsure how to use fscanf properly

This program should get in input a number and calculate the average value between a student's marks. This program works only with the former student, but not with the following ones. I think there's a mistake with that fscanf. Can anybody help?

int main ()
{
    FILE *cfPtr;
    int matricola_in, matricola, nEsami, codEsame, voto;
    char nome[20], cognome[20];
    int i, j, trovato = 0;
    float somma = 0.0;

    printf("Inserisci una matricola:\n");
    scanf("%d", &matricola_in);

    if( (cfPtr = fopen("studenti.txt", "r")) == NULL )
        printf("Errore");

    while( !feof(cfPtr) || trovato != 1 ){
        fscanf(cfPtr, "%d%s%s%d\n", &matricola, nome, cognome, &nEsami);
        if( matricola_in == matricola ){
            trovato = 1;
            for( i = 0; i < nEsami; i++ ){
                fscanf(cfPtr, "%d%d\n", &codEsame, &voto);
                somma += voto;
            }
            printf("Media: %.1f\n", somma/nEsami);
        }
    }

    fclose(cfPtr);

    return 0;
}

Edit: the data looks like:

matricola nome cognome n.esami`<eol>`
(for n.esami-rows)codice esame voto`<eol>`
...

Not clear but it seems the file contains a mix of line that have either four or two items.
Consider reading each line. Parse the line into up to four strings using sscanf. As needed use sscanf to capture the integers from the line. If there are two items, process them if trovato flag indicates a match has been found. If there are four items, see if there is a match and set trovato .

int main ()
{
    FILE *cfPtr;
    int matricola_in, matricola, nEsami, codEsame, voto;
    char nome[20], cognome[20];
    char temp[4][20];
    char line[200];
    int result;
    int i, j, trovato = 0;
    float somma = 0.0;

    printf("Inserisci una matricola:\n");
    scanf("%d", &matricola_in);

    if( (cfPtr = fopen("studenti.txt", "r")) == NULL ) {
        printf("Errore");
        return 0;
    }

    while( fgets ( line, sizeof line, cfPtr)){//read a line until end of file
        result = sscanf ( line, "%19s%19s%19s%19s", temp[0], temp[1], temp[2], temp[3]);//scan up to four strings
        if ( result == 2) {//the line has two items
            if ( trovato) {// match was found
                sscanf ( temp[0], "%d", &codEsame);
                sscanf ( temp[1], "%d", &voto);
                somma += voto;
            }
        }
        if ( result == 4) {//the line has four items
            if ( trovato) {
                break;//there was a match so break
            }
            sscanf ( temp[0], "%d", &matricola);
            sscanf ( temp[3], "%d", &nEsami);
            if( matricola_in == matricola ){//see if there is a match
                trovato = 1;//set the flag
            }
        }
    }
    printf("Media: %.1f\n", somma/nEsami);//print results

    fclose(cfPtr);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM