在c文件目录下准备好一个write_ata2020.txt,内容如下:
0.841471
0.909297
0.14112
-0.756802
-0.958924
-0.279415
0.656987
0.989358
0.412118
-0.544021
-0.99999
-0.536573
0.420167
0.990607
0.650288
-0.287903
-0.961397
-0.750987
0.149877
0.912945
C程序为:
#include <stdio.h>int main()
{double a[20];FILE *fpRead=fopen("write_ata2020.txt","r");if(fpRead==NULL){return 0;}for(int i=0;i<20;i++){fscanf(fpRead,"%lf ",&a[i]);printf("%lf \n",a[i]);}getchar();//等待return 1;
}
执行结果:
0.841471
0.909297
0.141120
-0.756802
-0.958924
-0.279415
0.656987
0.989358
0.412118
-0.544021
-0.999990
-0.536573
0.420167
0.990607
0.650288
-0.287903
-0.961397
-0.750987
0.149877
0.912945
附录例子:
读 float 格式 %f
读 double 格式 %lf
例子程序:
FILE *fp;
float x;
double y[10];
int i;
fp = fopen("abc.txt","r"); // 用读的方法打du开文件 abc.txt
fscanf(fp,"%f",&x); // 读 1 个 float 型的数zhidao
for (i=0;i<10;i++)
fscanf(fp,"%lf",&y[i]); // 读 10 个 double 型的数
fclose(fp); // 关文件
printf("x=%f\n",x);
printf("y:\n");
for (i=0;i<10;i++)
printf("%lf ",y[i]);