How do I get data from a file with fscanf, in c?

Asked

Viewed 91 times

0

Code:

while(fscanf(arq,"%d %d %d\n\r",x[i],y[i],raio[i])!=EOF){
    cout<<x[i]<<endl;
    i++;
}

Contents of the file:

mdb236rl    D   CALC B 912 2247 58
mdb240rl    D   CALC B 1752 776 95
mdb244rm    D   CIRC B 1940 1209 209
mdb248rl    F   CALC B 1805 1836 42
mdb252rm    F   CALC B 2743 1318 94
  • 1

    Which error appears, or which variable result?

1 answer

0


You need to read the text before the numbers con fscanf().

fscanf() will start searching for a number (because of the %d at the beginning of the formatting text) and soon find mdb236r1, which is not number. So, since you cannot read as number, you fail to EOF.

I think something like

char tmp[100];
while( fscanf(arq,"%s %s %s %s %d %d %d\n\r",tmp,tmp,tmp,tmp,x[i],y[i],raio[i]) != EOF ){
    cout<<x[i]<<endl;
    i++;
}

should work.

  • Thank you. I thought I would only take the values they had in the pattern I declared in fscanf, but have to put the same one in the file, and then just take the relevant ones. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.