我有一个简单的代码,它只读取一个格式为
的文件0.000000 17.437500
1.000000 17.441406
2.000000 17.451172
3.000000 17.458984
4.000000 17.468750
5.000000 17.486328
并且必须处理这些数据。我的问题是,即使在扫描数据之后,数组也没有与文本文件相同的数字,而是具有如下内容:
0.000000 0.000000
1.000000 0.480469
2.000000 0.000000
3.000000 0.482422
4.000000 0.000000
5.000000 0.486328
6.000000 0.000000
7.000000 0.490234
8.000000 0.000000
9.000000 0.498047
10.000000 0.000000
11.000000 0.507812
12.000000 0.000000
13.000000 0.515625
14.000000 0.000000
15.000000 0.527344
16.000000 0.000000
对我来说这看起来很奇怪,所有偶数都是0,我认为这可能是一个线索,但仍然无法找到问题。
这是我的代码,我已经和fscanf一起工作了,经过2天寻找错误来源并查看互联网后我仍然无法找到问题。
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fftw3.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_interp2d.h>
#include <gsl/gsl_spline2d.h>
const int N=360; // How many pairs of values in the file
const int DataFiles=6000; // How many data files are we going to read
double *irad;
int get_indexrad(int,int);
int main() {
int i, j, kii , kjj , index,t,t0;
double (*d_r)[N]= malloc(sizeof(d_r[0])*DataFiles);
FILE * conf;
FILE * op;
char file[100];
int jump=5;
// Initialization of the memory
irad = (double *) calloc(DataFiles*N, sizeof(double));
t0=4500;
/* open the file for reading and read the data into an array */
for (t=0; t<DataFiles; t++){
sprintf(file,"./interfase-%i.txt",t0+t*jump);
conf=fopen(file,"r");
if(conf == NULL){
perror("Could not open input file");
return -1;}
for(kii=0;kii<N;kii++){
index=get_indexrad(t,kii);
fscanf(conf,"%i %lf", &j, &irad[index]);
}
fclose(conf);
}
// Writing the first read file in a new txt to see if it was properly read
sprintf(file,"./Data/irad_t0.txt");
op=fopen(file,"w");
if(op == NULL){
perror("Could not create file");
return -1;}
t=0;
for(i=0;i<N;i++){
index=get_indexrad(t,i);
fprintf(op,"%f %lf\n", ((double)i), irad[index]);
}
fclose(op);
}
int get_indexrad(int t,int theta){
int index3;
index3=t*N + theta;
return index3;
}