c语言 怎么把float型的数写到文件中,这个涉及到文件操作。谢谢

2025-02-24 09:46:39
推荐回答(2个)
回答1:

用fprintf(文件地址,“%f”,float型变量名);就这样能写进去,比如文件是FILE *fp;
float fDat; 就是fprintf(fp,"%f",fDat);

回答2:

#include
#include

int main()
{
FILE *fp;
float x;
char name[50];

printf("Please input a float data:\n");
scanf("%f",&x);

printf("Please input a file name to create a file and store your float data:\n");
scanf("%s",name);
if((fp=fopen(name,"w"))==NULL) return -1;
fprintf(fp,"%g",x);
fclose(fp);
printf("Now,%g has been saved to %s.\n",x,name);

system("pause");
return 0;
}