需要帮助从 C 中读取 txt 文件
我正在开发一个 C 语言项目,该项目需要我从 txt 文件中读取矩阵值。前两行是行数和列数,其余是实际的矩阵数据。
例如,这样的事情:
2
2
1.0 2.0
3.0 4.0
我编写的代码给了我一些问题。这是一个片段:
matrix read(char* file){
FILE *fp;
printf("check 1\n");
fp = fopen(file,"r");
printf("file opened\n");
// Make the new matrix
matrix result;
printf("matrix created\n");
int counter = 0;
int i;
int j;
int holdRows;
int holdColumns;
if(counter == 0)
{ // read in rows
fscanf(fp, "%li", holdRows);
printf("here is holdRows: %li\n", holdRows);
counter++;
}
if(counter == 1)
{ // read in columns
fscanf(fp, "%li", holdColumns);
printf("here is holdColumns: %li\n", holdColumns);
counter++;
// Now that I know the dimensions, make the matrix
result = newMatrix(holdRows, holdColumns);
}
// For the rest, read in the values
for(i = 0; i < holdRows; i++)
for(j = 0; j < holdColumns; j++)
fscanf(fp, "%lf", &result->matrixData[i][j]);
fclose(fp);
return result;
}
每当我运行此命令时,holdRows 和holdColumns 都不是存储在txt 文件中的值。例如,我尝试了一个 3X4 矩阵,它显示有一行三列。
谁能告诉我我做错了什么?
谢谢 :)
I am working on a project in C which requires me to read in matrix values from a txt file. The first two lines are the number of rows and columns, and the rest is the actual matrix data.
For example, something like this:
2
2
1.0 2.0
3.0 4.0
The code I wrote is giving me some issues. Here's a snippet:
matrix read(char* file){
FILE *fp;
printf("check 1\n");
fp = fopen(file,"r");
printf("file opened\n");
// Make the new matrix
matrix result;
printf("matrix created\n");
int counter = 0;
int i;
int j;
int holdRows;
int holdColumns;
if(counter == 0)
{ // read in rows
fscanf(fp, "%li", holdRows);
printf("here is holdRows: %li\n", holdRows);
counter++;
}
if(counter == 1)
{ // read in columns
fscanf(fp, "%li", holdColumns);
printf("here is holdColumns: %li\n", holdColumns);
counter++;
// Now that I know the dimensions, make the matrix
result = newMatrix(holdRows, holdColumns);
}
// For the rest, read in the values
for(i = 0; i < holdRows; i++)
for(j = 0; j < holdColumns; j++)
fscanf(fp, "%lf", &result->matrixData[i][j]);
fclose(fp);
return result;
}
Whenever I run this, holdRows and holdColumns are not the values stored in the txt file. For example, I tried a 3X4 matrix, and it read that there was one row and three columns.
Can anyone tell me what I'm doing wrong?
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有将holdRows 和holdColumns 的地址传递给
fscanf
。您必须将它们更改为fscanf(fp, "%li", &holdRows);
和fscanf(fp, "%li", &holdColumns);
。You are not passing the address of holdRows and holdColumns to
fscanf
. You have to change them tofscanf(fp, "%li", &holdRows);
andfscanf(fp, "%li", &holdColumns);
.%li
转换规范需要一个long*
作为fscanf()
的匹配参数:您正在传递一个int
> (int*
经过 dbarbosa 提出的修正)。尝试
"%i"
...对于printf()
也是如此。%lf
需要一个double
。matrix
是由双精度数组成的吗?the
%li
conversion specification requires along*
as matching argument tofscanf()
: you're passing aint
(int*
after correction proposed by dbarbosa).Try
"%i"
... and same forprintf()
.The
%lf
expects adouble
. Ismatrix
made of doubles?尝试替换:
与
我似乎记得在 C 中某些类型的二维数组不能很好地与 & 配合使用。
Try replacing:
with
I seem to recall that in C some types of 2D arrays don't play nice with &.
感谢大家的建议和我自己的一些侦探工作,我解决了我的问题。首先,我输入了错误的文件名(好吧,现在我觉得很傻),其次,我读取的数据类型错误。
谢谢大家的帮助!
Thanks to suggestions by you all and some sleuth work myself, I solved my problem. First, I was entering the wrong filename (well, now I feel silly), and second, I was reading the data in as the wrong type.
Thanks, everyone, for your help!