【问题标题】:Reading in a text file with 5 lines to be filled into a 2D array in C用 5 行读取文本文件以填充到 C 中的二维数组中
【发布时间】:2015-03-01 22:51:36
【问题描述】:

我在读取文本文件时遇到问题,使用该信息形成一个二维数组,该数组将打印为一个矩阵。

具有这些值的文本文件:

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

第一个整数 (4) 将用于确定二维数组的行和列,例如。

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

如何使用代码的第一行(值:4)获取数组的维度,然后将剩余的值存储到数组中?

我尝试过这样做:

#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);

我想要的是:

1) 读取第一个值以确定二维数组的维度

2) 转到下一行

3) 将下一个值逐一读取并存入数组

4) 打印出来。

我该怎么做呢?

感谢您的阅读。

【问题讨论】:

    标签: c arrays file text 2d


    【解决方案1】:

    数组基本上是一个指向一堆值的指针,所以考虑一个 int 类型的数组 arr:

    arr[0];
    

    是一样的
    *arr;
    

    还有

    arr[2];
    

    一样
    *(arr+2);
    

    因此,如果您有一个 int 的二维数组,您实际上有一个指向 int 类型指针的指针。使用 malloc() 您可以使数组的大小动态化: (假设我们将行数读取到一个名为 'rows' 的变量中)

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

    arr 现在是一个指向 4 个整数数组的指针,可以照常访问:

    arr[row][col]
    

    希望能为您解决问题,谷歌“动态 2d 数组 c”了解更多信息 :)

    【讨论】:

    • 所以你的意思是,我处理数组的方式不是正确的方式,因为我试图将数据初始化为不存在的索引?
    【解决方案2】:

    你可以一次做一件事,而不是一个循环

    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++;
    }
    

    前面的回答中提到的数组也有问题

    【讨论】:

    • 快速提问,当在第 2 行和第 4 行时,“错误...”是否意味着在 if 语句的正文中,有一条消息说错误?或者代码有什么根本错误?
    • 只是 printf("error\n"); 的缩写在执行 atoi(pointer); 之类的操作之前进行错误检查很有用。无论如何,我将编辑代码以删除速记
    猜你喜欢
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 2021-12-14
    • 2019-03-24
    • 1970-01-01
    相关资源
    最近更新 更多