简体   繁体   English

如何正确使用fscanf和fgets从文件读取

[英]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. 我是C语言(这是我的第一语言)编程的新手,我的英语不太好,因此对不起语法...顺便说一句,我想知道您是否可以帮助我:从文件中读取数组的组件,将其设置为数组的一部分,并在屏幕中将其视为输出。 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 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? 我知道我的fscanf有一些问题(可能),我想知道malloc的使用是否正确...您能帮我吗? Where are the errors I make? 我在哪里犯了错误? How can I solve them? 我该如何解决? How should the code be using a fgets instead of fscanf? 代码应如何使用fgets而不是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. 好的,非常感谢您的答复,我将用一个长变量处理10位数字的电话号码,但是我仍然不知道如何正确使用fscanf。 It's the first time with delimitators, and this confuse me. 这是第一次使用分隔符,这使我感到困惑。 My format specifiers are "%s,%s,%s,%s,%ld,%s", right? 我的格式说明符是“%s,%s,%s,%s,%ld,%s”,对吗? but how should I put them in the fscanf? 但是我应该如何将它们放在fscanf中呢? What have I to write after the parameter "file" in the fscanf? 在fscanf中的参数“ file”之后我该写些什么?

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. 如果您告诉fscanf要读取的数据是用逗号分隔的,并且用方括号外的逗号进行处理,则无需指定要放入的内容( [^,] ),并且缺少格式说明符。 Try to provide fscanf the same format you gave to printf in the stampa function. 尝试为fscanf提供与stampa函数中给printf相同的格式。 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. 注意:整数变量不能正确容纳10位电话号码,请使用long变量或将其视为字符串。

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 . 另一方面, fgets会将整行读入字符串缓冲区,因为它不接受格式说明符,因此最终只能得到一个包含整个行的字符串,然后应使用sscanf这样的内容解析它使用fscanf方式相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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