需要帮助从 C 中读取 txt 文件

发布于 2024-09-19 00:28:20 字数 1233 浏览 4 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷你仙 2024-09-26 00:28:21

您没有将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 to fscanf(fp, "%li", &holdRows); and fscanf(fp, "%li", &holdColumns);.

鸠魁 2024-09-26 00:28:21

%li 转换规范需要一个 long* 作为 fscanf() 的匹配参数:您正在传递一个 int > (int* 经过 dbarbosa 提出的修正)。

尝试 "%i" ...对于 printf() 也是如此。


%lf 需要一个doublematrix 是由双精度数组成的吗?

the %li conversion specification requires a long* as matching argument to fscanf(): you're passing a int (int* after correction proposed by dbarbosa).

Try "%i" ... and same for printf().


The %lf expects a double. Is matrix made of doubles?

oО清风挽发oО 2024-09-26 00:28:21

尝试替换:

 for(i = 0; i < holdRows; i++)
       for(j = 0; j < holdColumns; j++)
             fscanf(fp, "%lf", &result->matrixData[i][j]);

 double temp;
 for(i = 0; i < holdRows; i++)
       for(j = 0; j < holdColumns; j++) {
             fscanf(fp, "%lf", &temp);
             result->matrixData[i][j] = temp;
       }

我似乎记得在 C 中某些类型的二维数组不能很好地与 & 配合使用。

Try replacing:

 for(i = 0; i < holdRows; i++)
       for(j = 0; j < holdColumns; j++)
             fscanf(fp, "%lf", &result->matrixData[i][j]);

with

 double temp;
 for(i = 0; i < holdRows; i++)
       for(j = 0; j < holdColumns; j++) {
             fscanf(fp, "%lf", &temp);
             result->matrixData[i][j] = temp;
       }

I seem to recall that in C some types of 2D arrays don't play nice with &.

半岛未凉 2024-09-26 00:28:20

感谢大家的建议和我自己的一些侦探工作,我解决了我的问题。首先,我输入了错误的文件名(好吧,现在我觉得很傻),其次,我读取的数据类型错误。

谢谢大家的帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文