简体   繁体   English

读取具有5行的文本文件以填充到C中的2D数组中

[英]Reading in a text file with 5 lines to be filled into a 2D array in C

I am facing a problem with reading in a text file, using that information to form a 2D array which will print out to be a matrix. 我在读取文本文件时遇到了问题,使用该信息形成一个2D数组,该数组将打印为矩阵。

Text file with these values: 具有以下值的文本文件:

4
9 12 13 1
7 3 6 9
8 4 2 1
10 5 15 3

The very first integer (4) will be used to determine the rows and columns of the 2D array, ex. 第一个整数(4)将用于确定2D数组的行和列,例如。

int row = 4;
int col = 4;
int matrix[row][col]; //will be a 4x4 matrix

How can I use the first line of the code (value: 4) to get the dimensions of the array but then store the remaining values into the array? 如何使用代码的第一行(值:4)获取数组的尺寸,然后将其余值存储到数组中?

I've tried to do something like this: 我试图做这样的事情:

#define ARRAYSIZE 10000
char *matDimen;
char *tokenArray;
char buffer[ARRAYSIZE];


FILE *file = fopen("textfile.txt", "r");

while (fgets(buffer, sizeof buffer, file) != NULL)
{
    if (row == 0) //Take the first value read to determine the dimension of the array
    {
        matDimen = strtok(buffer, " "); //Using strtok to read in the first value as a string
        row = atol(matDimen); //Converting the string into an int
        col = row; //Because rows and columns are going to be the same, initialize column to row

        if (row < 1 || 10 < row)
        {
            if (col < 1 || 10 < row)
                {
                    printf("Matrix must be within 2x2 and 9x9.\n");
                    exit(0);
                }
        }
    }
    for (i = 0; i < row; i++) //Ideally, assigns values from the text file into an index of the array
    {
        for (j = 0; j < col; j++)
        {
            tokenArray = strtok(NULL, " ");
            //strcpy(matrix[i][j], tokenArray); Program crashes here
            //printf("%s ", matrix[i][j]);
        }

    if (j == col) //Formats the matrix into a 4x4 square, in this scenario
    {
        printf("\n");
    }
}
fclose(file);

What I want is: 我想要的是:

1) Read the very first value to determine the dimension of the 2D array 1)读取第一个值以确定2D数组的维数

2) Go to the next line 2)转到下一行

3) Read the next values one by one and store them into the array 3)逐一读取下一个值并将其存储到数组中

4) Print it out. 4)打印出来。

How can I go about doing this? 我该怎么做呢?

Thank you for reading. 感谢您的阅读。

An array is basically a pointer to a bunch of values, so consider an array arr of type int: 数组基本上是指向一堆值的指针,因此请考虑int类型的数组arr:

arr[0];

Is the same thing as 是一样的东西

*arr;

And

arr[2];

Is the same as 是相同的

*(arr+2);

Thus, if you have a 2D array of int, you actually have a pointer to a pointer of type int. 因此,如果您有一个int的2D数组,则实际上您有一个指向int类型的指针的指针。 Using malloc() you can then make the array dynamic in size: (Suppose we read the number of rows to a var called 'rows') 然后,使用malloc()可以使数组的大小动态:(假设我们将行数读取到称为“行”的var中)

int** arr = malloc(sizeof(int[4])*rows);

arr now is a pointer to an array of 4 integers, that can be accessed as usual: arr现在是一个指向4个整数的数组的指针,可以照常访问它:

arr[row][col]

Hope that clears it up for you, Google "dynamic 2d array c" for more info :) 希望能为您解决问题,有关更多信息,请使用Google“动态2d数组c” :)

You can do things one at a time instead of one loop 您可以一次做一件事情,而不是一个循环

fgets(buf, sizeof buf, file);
if (!buf) printf("error 1...\n");
row = atoi( buf ); 
if ( row < 1 || row > 10 || col < 1 || col > 10 )  printf("error 2...\n");

int r = 0;
while (fgets(buf, sizeof buf, file) )
{
   char *p = strtok( buf, " " );
   for (int column = 0; column < row; column++)
   {
      if (!p) break;//error
      matrix[r][column] = atoi(p);
      p = strtok (NULL, " ");
   }
   r++;
}

Also there is a problem with arrays as mentioned in previous answer 如前一个答案中所述,数组也存在问题

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

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