我正在尝试将双数组写入文件并再次读取它们。下面是我的代码,但我缺少一些东西。这听起来很傻,但我无法做到。
#include <stdio.h>
#include <stdlib.h>
int main(){
int i,j,k;
int N = 10;
double* readIn = new double[N];
double* ref = new double[N];
FILE* ptr1, *ptr2;
ptr1 = fopen("output.txt","w");
//write out
for (i = 0; i < N;i++){
ref[i] = (double)i;
fprintf(ptr1,"%g\n",(double)i);
}
fclose(ptr1);
//read file
ptr2 = fopen("output.txt","r+");
//read in
for(i = 0;i < N;i++)
fscanf(ptr2, "%g", &readIn[i]);
fclose(ptr2);
for(i = 0;i<N;i++)
if(ref[i] != readIn[i]){
printf("Error: %g %g\n",ref[i], readIn[i]);
}
return 0;
}