怎么读取矩阵数据文件
如何读取矩阵数据文件?
我的问题是想把matrix.txt中的数据读取到一个变量中,代码如下
#include"stdio.h"
#include "stdlib.h"
void main()
{
FILE *fp;
double *matrix;
int row = 4, col = 4;
int i, j;
fp = fopen("matrix.txt‘,"rb");
matrix = (double *) malloc( row * col * sizeof(double));
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
fscanf(fp,"%e", &matrix[i * col + j]) //这里我想用科学计数的方式读入。主要matrix.txt中是以科学计数的方式存贮的。
}
}
fclose(fp);
free(matrix);
}
代码大致如上面所示,请问我为什么得不到matrix.txt中的数据呢,各位帮忙,急啊!
martix.txt 中的数据如下:
3.761171e-023 7.492343e-003 0.000000e+000 4.991528e-060
6.982327e-003 6.183527e-024 0.000000e+000 2.622026e-047
1.566381e-043 1.959441e-057 0.000000e+000 3.894469e-003
2.903130e-052 6.453233e-075 0.000000e+000 1.229115e-041
------解决方案--------------------
fscanf(fp,"%e", &matrix[i * col + j])
改为
fscanf(fp,"%le", &matrix[i * col + j])
------解决方案--------------------
1楼正确
我的问题是想把matrix.txt中的数据读取到一个变量中,代码如下
#include"stdio.h"
#include "stdlib.h"
void main()
{
FILE *fp;
double *matrix;
int row = 4, col = 4;
int i, j;
fp = fopen("matrix.txt‘,"rb");
matrix = (double *) malloc( row * col * sizeof(double));
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
fscanf(fp,"%e", &matrix[i * col + j]) //这里我想用科学计数的方式读入。主要matrix.txt中是以科学计数的方式存贮的。
}
}
fclose(fp);
free(matrix);
}
代码大致如上面所示,请问我为什么得不到matrix.txt中的数据呢,各位帮忙,急啊!
martix.txt 中的数据如下:
3.761171e-023 7.492343e-003 0.000000e+000 4.991528e-060
6.982327e-003 6.183527e-024 0.000000e+000 2.622026e-047
1.566381e-043 1.959441e-057 0.000000e+000 3.894469e-003
2.903130e-052 6.453233e-075 0.000000e+000 1.229115e-041
------解决方案--------------------
fscanf(fp,"%e", &matrix[i * col + j])
改为
fscanf(fp,"%le", &matrix[i * col + j])
------解决方案--------------------
1楼正确