简体   繁体   中英

How to properly use fscanf and fgets to read from file

I'm new at programming in C (it's my first language) and I'm not so good at english, so sorry for the grammar... By the way, I was wondering if you could help me with this: I have to read from file the components of the array, setting them as part of it and see them as output in the screen. I created a file with this informations:

Rossi,Mario,M,mariorossi@gmail.com,3923333332,Portiere Bianchi,Giuseppe,M,giuseppebianchi@gmail.com,3470000021,Attaccante Ferrari,Anna,F,annaferrari@gmail.com,3466482645,Attaccante Romano,Antonio,M,antonioromano@gmail.com,3450394672,Centrocampista

and trying to coding I ended up with this:

    #include <stdio.h>
    #include <stdlib.h>
    struct dati_giocatori {
        char cognome[20];
        char nome[20];
        char genere[20];
        char email[20];
        int telefono;
        char ruolo[20];
    };

    typedef struct dati_giocatori GIOCATORE;

    void lettura(FILE *file, GIOCATORE *vettore, int dim);
    void stampa(GIOCATORE *vettore, int dim);

    int main (){
        FILE *file;
        GIOCATORE *vettore;
        int dim;

        vettore=(GIOCATORE*)malloc(dim*sizeof(GIOCATORE));
        printf("how many players do you want to see?");
        scanf("%d",&dim);


        file=fopen("Giocatori.txt","r");

        lettura(file,vettore,dim);
        stampa(vettore,dim);

        system("pause");
        return 0;

    }

    void lettura(FILE *file, GIOCATORE *vettore, int dim){
        int i=0;
        if(file){
                while(!feof(file) && i<dim){
                    fscanf(file,"%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],[^\n]", vettore[i].cognome,vettore[i].nome,vettore[i].genere,vettore[i].email,&vettore[i].telefono,vettore[i].ruolo);
                i++;
                }
            }
            else{
                printf("Error.");
            }
    }


    void stampa(GIOCATORE *vettore, int dim){
        int i=0;
        while(i<dim){
            printf("%s,%s,%s,%s,%d,%s\n", vettore[i].cognome,vettore[i].nome,vettore[i].genere,vettore[i].email,vettore[i].telefono,vettore[i].ruolo);
        i++;
        }
    }

but it does not work very well. I know I have some problems with the fscanf (probabily), and I wanted to know if the malloc is used correctly... Could you help me? Where are the errors I make? How can I solve them? How should the code be using a fgets instead of fscanf? Am I using the delimitators in the right way?

Ok, thank you very much for replying, I'll treat the 10-digit phone number with a long variable, but I still don't get how I should correctly use the fscanf. It's the first time with delimitators, and this confuse me. My format specifiers are "%s,%s,%s,%s,%ld,%s", right? but how should I put them in the fscanf? What have I to write after the parameter "file" in the fscanf?

fscanf(file,"%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],[^\n]",...

Your format specifiers are not correct; if you tell fscanf that the data to read is comma-separated, which you do with the commas outside the brackets, you don't need to specify what you put inside ( [^,] ), and you're missing format specifiers. Try to provide fscanf the same format you gave to printf in the stampa function. Be careful: an integer variable would not hold correctly a 10-digit phone number, use a long variable or treat it like a string as well.

On the other hand, fgets would read the whole line into a string buffer as it does not accepts a format specifier, so you would end up having a one string only, containing the entire line, then you should parse it with something like sscanf in the same way you should use fscanf .

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