简体   繁体   English

将文本文件输入到c中的2d数组中

[英]input text file into an 2d array in c

#include<stdlib.h>
#include<stdio.h>

int main() {

  FILE* file = fopen ("fourth.txt", "r");
  int i = 0;
  int rows=0;
  int rows2=0;
  int columns=0;
  int counter=0;
  int length=0;
  int multiply=0;

  fscanf (file, "%d", &i);
  rows=i;

  printf("the rows are %d\n", i);

  int matrix[rows][rows];
  length=rows*rows;

  if (rows<=0) {

      printf("error, this file cannot be processed");
      return 0;
  }

  do
  {
      if (fscanf(file, "%d", &i)==1) {

          if (counter<length) {

              matrix[rows2][columns]=i;
              counter++;
              columns++;

              if (columns==rows) {
                  rows2++;
                  columns=0;
              }
          }
          multiply=i;
      }
  } while (!feof(file));

  fclose (file);

  for ( int c = 0; c < rows; c++ ) {
      for ( int j = 0; j < rows; j++ ) {
          printf("matrix[%d][%d] = %d\n", c,j, matrix[c][j] );
       }
  }

  printf("the multiply is %d", multiply);

I am trying to create a matrix exponential (matrix multiplication in another words). 我正在尝试创建矩阵指数(换句话说,矩阵乘法)。 However before I perform the actual mathematical process, I have to read numbers as input from a txt file and store them in a 2d array. 但是,在执行实际的数学过程之前,我必须从txt文件中读取数字作为输入并将其存储在2d数组中。 Above is what I have from the code so far, however, I am struggling to actually input the numbers into the array because when I was testing my 2d array out at the very end, I was getting odd results. 以上是到目前为止我从代码中获得的信息,但是,我很难将数字实际输入到数组中,因为当我在最后测试2d数组时,我得到的结果很奇怪。

for example: 1. Inputs: 例如:1.输入:

3 3
123 123
456 456
789 789
2 2

The 3 represents the number of rows, which is the same as the number of columns for this particular 2d array because the matrix is square, meaning all rows and columns are the same. 3表示行数,与该特定2d数组的列数相同,这是因为矩阵是正方形的,这意味着所有行和列都相同。

The 2 represents the exponent, which is the number of times I have to multiply the original matrix by itself. 2代表指数,这是我必须乘以原始矩阵的次数。

However my output is 但是我的输出是

matrix[0][0] = 123 矩阵[0] [0] = 123
matrix[0][1] = 456, 矩阵[0] [1] = 456,
matrix[0][2] = 789 矩阵[0] [2] = 789
matrix[1][0] = 2 矩阵[1] [0] = 2
matrix[1][1] = 4214800 矩阵[1] [1] = 4214800
matrix[1][2] = 0 矩阵[1] [2] = 0
matrix[2][0] = 3 矩阵[2] [0] = 3
matrix[2][1] = 0 矩阵[2] [1] = 0
matrix[2][2] = 0 矩阵[2] [2] = 0

Expected output has to be: 预期输出必须为:

123 123
456 456
789 789

in a 2d array 在二维数组中

Is there a reason why? 有什么理由吗?

Also how would I modify the code so no matter what the input is in the txt file regarding different rows and columns, I could still format the proper 2d array. 另外,我将如何修改代码,以便无论txt文件中关于不同行和列的输入是什么,我仍然可以格式化适当的2d数组。 Thanks 谢谢

In your input text file, the digits in a line are not separated in any way. 在输入文本文件中,一行中的数字不会以任何方式分开。 If you need multi-digit numbers, consider separating them by a symbol like space or semicolon. 如果需要多位数字,请考虑使用空格或分号之类的符号将其分开。

If the matrix has only single digit whole numbers (ie, numbers from 0 to 9), it would be fine. 如果矩阵只有一位整数(即从0到9的数字),那就很好了。

But if there the 3 numbers in a line are, say, 21, 13 and 4, in the input file it would be just 21134 . 但是如果一行中的3个数字分别是21134和4,那么在输入文件中它将仅为21134

There's no way to find if it was 211,3,4 or 21,1,34 or 2,113,4 or ....... 无法找到它是211,3,4还是21,1,34或2,113,4或.......

With the fscanf() in your program, each %d will read the entire line considering it to be a single number and assign it to the variable i . 在程序中使用fscanf()时,每个%d都会读取整行,并将其视为单个数字,并将其分配给变量i

Instead of reading the size first into i and then copying it to rows like 而不是先读大小到i ,然后将其复制到rows

fscanf (file, "%d", &i);
rows=i;

you could directly read into rows 您可以直接读入rows

fscanf( file, "%d", &rows);

And read the values like 并读取类似的值

for(rows2=0; rows2<rows; ++rows2)
{
    if( fscanf(file, "%1d%1d%1d", &matrix[rows2][0], &matrix[rows2][1], &matrix[rows2][2]) != 3)
    {
        perror("Error");
        break;
    }
}

Then read the value for multiply at the end like 然后像下面那样读取要multiply的值

fscanf(file, "%d", &multiply);

You may check the return value of fscanf() to check if any error occurred. 您可以检查fscanf()的返回值以检查是否发生任何错误。

You used !feof(file) to check end of file. 您使用!feof(file)检查文件结尾。 I don't know much but have a look here . 我不太了解,但在这里看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM