关于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;
}
[解决办法]
#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;}