scanf - double value cant read by fscanf in c -
# 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 file trans.dat existed or not printf("cannot open file. \n"); exit(1); } fscanf(stdin,"%d%f",&accno,&balance);/* read keyboard */ fprintf(tfptr,"%d %f",accno,balance);/* write file */ fclose(tfptr); //close file trans.dat if((tfptr=fopen("trans.dat","r"))==null){ //check file trans.dat file existed or not printf("cannot open file. \n"); exit(1); } fscanf(tfptr,"%d%f",&accno,&balance);/* read file */ fprintf(stdout,"%d %f",accno,balance);/* write screen */ return 0; } output:
12 2.3 12 0.000000 -------------------------------- process exited return value 0 press key continue . . .
the "%f" format behaves differently between scanf , printf.
for scanf (and family) "%f" reads float variable, not double. if want read double need "%lf" format scanf.
Comments
Post a Comment