Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c中从文本文件读取浮点值时出现错误_C_File_Scanf - Fatal编程技术网

在c中从文本文件读取浮点值时出现错误

在c中从文本文件读取浮点值时出现错误,c,file,scanf,C,File,Scanf,我正在读取包含如下值的文本文件 125602365 119653955 126374444 124463807 127312438 128395899 和下面的代码来阅读它 if(!(pvtcheck(0))) { fscanf(fp,"%f",&deltime); printf("\ndeltime0=%f\n",deltime); ///Actual value is 125602365, but i am getting 125602368. }

我正在读取包含如下值的文本文件

125602365 119653955 126374444 124463807 127312438 128395899

和下面的代码来阅读它

if(!(pvtcheck(0)))
  {
     fscanf(fp,"%f",&deltime);
     printf("\ndeltime0=%f\n",deltime);    ///Actual value is 125602365, but i am getting 125602368.
  }

  if(!(pvtcheck(1)))
  {
      fscanf(fp,"\t%f",&deltime); 
      printf("\ndeltime1=%f\n",deltime);///Actual value is 119653955, but i am getting 119653952.
  }
  /// same for pvtcheck(2),pvtcheck(3),pvtcheck(4)
  if(!(pvtcheck(5)))
  {
      fscanf(fp,"\t%f",&deltime);
       printf("\ndeltime5=%f\n",deltime); ///Actual value is 128395899, but i am getting 128395896.
  }

在我编写的注释输出中,任何不更改值的解决方案都需要使用
double
类型,而不是
float
,以及
%lf
格式说明符,用于
scanf
而不是
%f

double deltime;

fscanf(fp, "%lf", &deltime);
printf("\ndeltime0=%lf\n", deltime);
fscanf(fp, "\t%lf", &deltime);
printf("\ndeltime1=%f\n", deltime);
...
顺便说一句:对于
printf
,您可以使用
%f
%lf
两者的含义相同


另请阅读

deltime的类型是什么?请学习如何创建一个。在几个不相关的注释上:您不需要条件表达式中的所有括号,例如,
if(!pvtcheck(0))
就足够了。此外,不要在
if
主体中重复三次相同的操作,而是先做所有常见的操作,然后使用条件来完成不同的操作。最后,不需要
fscanf
格式中的选项卡
“\t”
。请了解有关格式设置的信息,或者不要尝试(许多用户乐于帮助设置格式),您的尝试已破坏了C语法。学习缩进也会很有帮助。请学习并应用制作缩进的概念。请花一些时间阅读,尤其是命名为和的部分。也请和。再一次,请学习如何创建一个。很好的参考@展开有
%lf
,其含义与
%f
相同。请参阅C标准的第7.21.6.1节(“fprintf函数”)@M.M谢谢,我只检查了手册页,当然不太完整。