首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于fsacnf函数读小数精度的有关问题

2012-05-28 
关于fsacnf函数读小数精度的问题源代码如下:txt文件里的内容是一行小数:21.74231 334.74332 3.74343 2.743

关于fsacnf函数读小数精度的问题
源代码如下:
txt文件里的内容是一行小数:21.74231 334.74332 3.74343 2.74343 32.44356
但是执行完下面的代码后运行结果却是:21.7423 334.743 3.74343 2.74343 32.4436
怎样才能准确的读出来呢?求指教!
#include "stdafx.h"
#include <iostream>#include <fstream>
#include<stdio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

 
  
double temp = 0; 
  double num[5];
FILE *fp = fopen("C:\\Users\\Administrator\\Desktop\\temp.txt","r");
for(int i=0;i<5;i++)
{
if (fp!=NULL)
{
fscanf(fp,"%lf",&temp);
}
num[i]=temp;
}


 
for(int j=0;j<5;j++)
{

cout<<num[j]<<" ";
}
 
fclose(fp);
return 0;
 



}



[解决办法]

C/C++ code
#include <iostream>#include <fstream>#include<stdio.h>#include<iomanip>using namespace std;int _tmain(int argc, _TCHAR* argv[]){    double temp = 0;      double num[5];    FILE *fp = fopen("test.txt","r");    for(int i=0;i<5;i++)    {        if (fp!=NULL)        {            fscanf(fp,"%lf",&temp);        }        num[i]=temp;    }    cout.setf(ios::fixed);    for(int j=0;j<5;j++)    {        cout<<num[j]<<" ";    }    fclose(fp);    return 0;} 

热点排行