关于scanf函数的问题
我c语言的编译环境是vc++6.0,
我要读入一个文件的数据,文件内容为: abc 40 50
我用scanf(fp,“%s %d %d”,s,&a,&b);其中s为一数组,a,b定义为int型
我可以读入数据,没有发生错误
当我把文件内容改为:abc 40.2 50.6时
我用scanf(fp,“%s %f %f”,s,&a,&b);其中s为一数组,a,b定义为double型
就会发生错误!!!
我的问题是scanf函数是不是不能读入小数。。。。
谢谢高手给予指点!!
[解决办法]
scanf(fp,“%s %lf %lf”,s,&a,&b);
%f是输入float,%lf输入double
[解决办法]
你被忽悠了~~
#include <stdio.h>#include <stdlib.h>int main(){ FILE *fp=fopen("data","w"); if (fp==NULL) { printf("open file error!\n"); } fprintf(fp,"%s %f %f","abc",40.2,50.6); fclose(fp); fp=fopen("data","r"); if (fp==NULL) { printf("open file error!\n"); } char str[10]; double d1,d2; fscanf(fp,"%s %lf %lf",str,&d1,&d2); printf("%s %f %f\n",str,d1,d2); return 0;}