简体   繁体   English

跳过文件的第一行,该行的单词在C中用逗号分隔

[英]Skip the first line of the file which has words separated by commas in C

I have a .txt file that contains words and numbers like this 我有一个.txt文件,其中包含这样的单词和数字

ID;Name;Surname;Phone;State;

And what i am trying is to skip this line, located at the begining, i have looked for it but i have no seen any case in which the words are separated by commas. 我正在尝试跳过开头的这一行,尽管我一直在寻找它,但是我没有看到单词之间用逗号分隔的情况。 I am using fscanf like this 我正在像这样使用fscanf

void printCustomers(){
FILE *f;
f=fopen("people.txt","r");
if(f==NULL){
    printf("Error");
}else{
    do{
        char id[5],name[11],surname[26],phone[30],state[10]
        fscanf(f,"%4[^;];%10[^;];%25[^;];%50[^;];%9[^;]\n",id,name,surname,phone,state);
        printf("ID:%s\nName:%s\nSurname: %s\nPhone: %s\nCity: %s\nState: %s\n",id,name,surname,phone,state);
    }while(!feof(f));
fclose(f);
}

The file is like this: 该文件是这样的:

ID;Name;Surname;Phone;State;1234;Harry;Ramirez;9874134;OT

What i want to skip is only this part 我只想跳过的部分

ID;Name;Surname;Phone;State;

I have tried using fgets but without success. 我尝试使用fgets,但没有成功。 I have also observed that when it prints the first lines, it writes strange letters, maybe because there is something else wrong in my code, which is more than likely. 我还观察到,当它打印第一行时,它会写一些奇怪的字母,这可能是因为我的代码中存在其他错误,这很可能是错误的。 Any ideas? 有任何想法吗? Thanks 谢谢

Ok, i tried again with fgets and i don't know why, it works! 好的,我再次尝试使用fgets,但我不知道为什么,它有效! I've also change some parts of the code and added more vars. 我还更改了代码的某些部分,并添加了更多的变量。 Here it is just if someone finds it useful. 在这里,只是有人觉得它有用。

void printCustomers(){
FILE *f;
f=fopen("clientes.txt","r");
char fila1[122];
if(f==NULL){
    printf("No se ha podido leer el archivo");
}else{
    fgets(fila1,121,f);
    do{
        char id[6],name[11],surname[26],address[51],city[26],state[3],postalcode[6];
        fscanf(f,"%5[^;];%10[^;];%25[^;];%50[^;];%25[^;];%2[^;];%s\n",id,name,surname,address,city,state,postalcode);
        printf("ID:%s\nName:%s\nSurname: %s\nAddress: %s\nCity: %s\nState: %s\nPostalcode: %s\n",id,name,surname,address,city,state,postalcode);
        printf("\n");
    }while(!feof(f));
fclose(f);
}

Thanks for all! 谢谢大家!

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

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