0

I have a .txt file that contains words and numbers like this

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

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. 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

Dragonred
  • 1
  • 3
  • 1
    "I have tried using fgets but without success." – specifically how? – The Paramagnetic Croissant May 18 '15 at 22:48
  • Basically, nothing happened and when i execute it, it stops working – Dragonred May 18 '15 at 22:52
  • Can you please clarify your question? What do you mean "skip this words"? What "this words" does that refer to? And you mention "commas" but it is not clear what that has to do with the problem you are trying to solve. Perhaps give an example. – kaylum May 18 '15 at 23:01
  • 4
    There are 5 fields in your header line, then 10 fields. You have declared 5 `char` arrays. The `fscanf` format specifiers ask for 6, but takes 7 arguments. Then there are 6 `printf` format specifiers but only 5 arguments provided. That's why you get strrange letters! And `pone` is not `phone`. Lastly never use `feof`, control the loop with the result from `fscanf`. http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Weather Vane May 18 '15 at 23:29
  • If each line is ;-delimited, for better error handling you are probably better off not using fscanf but fgets. (Not sure what you mean "nothing happened" - did you at least confirm that the line was read?) Once the line is read you can write a helper to split at ; and put the result into an array, which also help you better deal with missing or malformed entries. – Ron Kuper May 19 '15 at 00:18
  • Suggest `fscanf(f,"%4[^;];%10[^;];%25[^;];%50[^;];%9[^;];\n"` (add final ;) – chux - Reinstate Monica May 19 '15 at 04:15

1 Answers1

0

Ok, i tried again with fgets and i don't know why, it works! 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!

Dragonred
  • 1
  • 3