c中的fscanf无法读取双值
c中的fscanf无法读取双值,c,scanf,C,Scanf,“%f”格式在和之间的行为不同 对于scanf(和系列),%f读入浮点变量,而不是双精度。如果你想读一个double,那么你需要scanf的%lf格式,尽管它们属于同一个家族,OP询问的是fscanf(),而不是scanf() # include <stdio.h> # include <io.h> # include <stdlib.h> int main(int argc,char *argv[]){ FILE *tfptr; int acc
“%f”
格式在和之间的行为不同
对于
scanf
(和系列),%f
读入浮点
变量,而不是双精度
。如果你想读一个double
,那么你需要scanf
的%lf
格式,尽管它们属于同一个家族,OP询问的是fscanf()
,而不是scanf()
# include <stdio.h>
# include <io.h>
# include <stdlib.h>
int main(int argc,char *argv[]){
FILE *tfptr;
int accno;
double balance;
if((tfptr=fopen("trans.dat","w"))==NULL){ //check the file trans.dat existed or not
printf("cannot open file. \n");
exit(1);
}
fscanf(stdin,"%d%f",&accno,&balance);/* read from keyboard */
fprintf(tfptr,"%d %f",accno,balance);/* write to file */
fclose(tfptr); //close the file trans.dat
if((tfptr=fopen("trans.dat","r"))==NULL){ //check the file trans.dat file existed or not
printf("cannot open file. \n");
exit(1);
}
fscanf(tfptr,"%d%f",&accno,&balance);/* read from file */
fprintf(stdout,"%d %f",accno,balance);/* write to screen */
return 0;
}
12 2.3
12 0.000000
--------------------------------
Process exited with return value 0
Press any key to continue . . .