简体   繁体   中英

using fscanf to read stream of data from file

I need to read a steam of data from file, it looks like :

0   1.8750  274.90  0   0   0
1   3.7500  370.50  0   1   0
2   7.5000  463.50  0   2   0

so I use :

fscanf(p,"%d%f%f%d%d%d",&a, &b,&c,&d,&t,&q);

when I try to see if it reads correctly, I used:

printf("A %d",a);
printf("B %f",b);

the output :

A 0B -1.882607

So, why fscanf doesn't read float correctly ??

The likely source of the problem is mis-match fscanf() directives
or
incomplete fscanf() read. Maybe due to an unopened file or unexpected input text.

A 0B -1.882607
| || +--------- b value -1.882607
| |+----------- Letters B space
| +------------ a value 0
+-------------- Letters A space

Insure your variables are the following types:

int a,d,t,q;
float b,c;

Check the result of the fscanf()

int result = fscanf(p,"%d%f%f%d%d%d",&a, &b,&c,&d,&t,&q);
if (result != 6) {
  printf("Error, unexpected return value:%d\n", result);
}

Additional info form the OP would help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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