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

0

Code:

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

File content:

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
    
asked by anonymous 19.02.2018 / 14:15

1 answer

0

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

fscanf() will start by searching for a number (because of %d at the beginning of the formatting text) and then find mdb236r1 , which is not number. So, being unable to read as number, it fails with 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.

    
14.03.2018 / 15:45