我有一个包含大量数字的文件,例如-7.47004e-16
,我正在尝试使用将其读入浮点数组
fscanf(rhs, "%f", &numbers[i]);"
这是在一个while循环中。但是当我们有一个上面提到的数字时,这不起作用。
由于数量太大,这不起作用吗?还是数字格式中的“e”不起作用?
您能推荐一些正确执行此操作的方法吗?
谢谢。
注意:Numbers 是一个浮点数组,rhs 是文件名。该文件每行有一个数字,一些数字与上面的格式相同,一些数字小得多,例如-1.88493
.
这是代码:
int main( int argc, char *argv[])
{
FILE *rhs, *output;
int niter, n, n1;
// counters
int i = 0, j = 0, k, m, p;
rhs = fopen(argv[1], "r");
// ab+ opens file for writting and creates the file if need be
output = fopen(argv[2], "ab+");
niter = atoi(argv[3]);
// check if files open up or not, if not exit.
if((rhs == NULL) || (output == NULL))
{
printf("Error Opening files.\n");
exit(1);
}
// read in N
fscanf(rhs, "%d", &n);
// initialize n1
n1 = n + 1;
// generate array to hold values from rhs file
long double *numbers = (long double *)malloc(sizeof(long double) * ((n1)*(n1)));
long double *y = (long double *)malloc(sizeof(long double) * ((n1)*(n1)));
long double *f = (long double *)malloc(sizeof(long double) * ((n1)*(n1)));
long double *yp = (long double *)malloc(sizeof(long double) * ((n1)*(n1)));
// get numbers and store into array
for(i = 0; i <= n; i++)
{
for(j = 0; j <= n; j++)
{
fscanf(rhs, "%Lf", &numbers[i]);
printf("i = %d, number = %Lf\n", i, numbers[i]);
}
}
for(k = 0; k < niter; k++)
{
smooth(n, y, yp, f);
}
fclose(rhs);
free(numbers);
free(y);
free(f);
free(yp);
return 0;
}