如何简单地将浮点数转换为c中的字符串?
发布时间:2020-12-16 10:04:53 所属栏目:百科 来源:网络整理
导读:FILE * fPointer;float amount = 3.1415;fPointer =fopen("vending.txt","w");fprintf(fPointer,amount);printf("The file has been created for the first time and we added the value %f",amount);fclose(fPointer); 我试图将浮点数保存到文本文件但是当
FILE * fPointer; float amount = 3.1415; fPointer =fopen("vending.txt","w"); fprintf(fPointer,amount); printf("The file has been created for the first time and we added the value %f",amount); fclose(fPointer); 我试图将浮点数保存到文本文件但是当我尝试运行此代码时它会触发编译错误,因为函数fprintf期望第二个参数是一个字符数组,所以我怎么能将我的浮点数转换为字符串所以我可以传递它,我来自ac #background,类似.toString()的东西是可能的,所以在c中是否有类似的东西直接将浮点数转换为字符串? 解决方法
第二个参数是格式字符串,格式参数遵循以下格式:
fprintf(fPointer,"%f",amount); %f告诉fprintf将此参数(amount)写为float值的字符串表示形式. 您可以在MSDN中找到文档和示例. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |