【问题标题】:reading a text file of non-even lines into 2D array将非偶数行的文本文件读入二维数组
【发布时间】:2013-11-18 02:41:57
【问题描述】:

抱歉,我对 C 语言很陌生,我遇到了困难。我有一个输入文本文件,内容如下:

5 3
383 386 287
415 293 335
368 492 149
421 362 27
190 59 263

我正在尝试将其读入二维数组。我正在尝试的是:

FILE * fin = NULL;
fin = fopen("myTestData.txt", "r");
int twod[MAX_ROWS][MAX_COLS];

int i, j, num, row, col;
fscanf(fin, "%d%d", &row, &col);

fclose(fin);

fin = fopen("myTestData.txt", "r");
for(i = 0; i < row; i++)
{
    for(j = 0; j < col; j++)
    {
        fscanf(fin, "%i ", &num);
    twod[i][j] = num;
    }
}

我遇到的问题是,在空白为 (twod[0][2]) 的第一行上,它为其分配了第二行 (383) 的第一个整数的值。我该怎么做才能使 [0][2] 获得空值?

感谢您的帮助

【问题讨论】:

  • 您可以逐行读取输入,并将其标记为数字。如果数字较少,则为其余元素分配虚拟值。
  • 顺便说一句,第一行似乎指定了输入数据中的行数和列数。这就是您的代码使用它的方式(请参阅如何为 row 和 col 分配值)。因此,您只需从for 循环中的第二行开始阅读。 C 中的整数没有 null 值。

标签: c arrays 2d


【解决方案1】:

删除您关闭并重新打开文件的行。读入行数和列数后,您只需要处理剩余的数据,这些数据将具有良好的结构 - 就像大多数作业问题一样。

FILE * fin = NULL;
fin = fopen("myTestData.txt", "r");
int twod[MAX_ROWS][MAX_COLS];

int i, j, num, row, col;
fscanf(fin, "%d%d", &row, &col);

//fclose(fin);

//fin = fopen("myTestData.txt", "r");
for(i = 0; i < row; i++)
{
    for(j = 0; j < col; j++)
    {
        fscanf(fin, "%i ", &num);
        twod[i][j] = num;
    }
}

for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
        printf("%d ", twod[i][j]);
    printf("\n");
}

【讨论】:

    【解决方案2】:
        #include <stdio.h>
        #include <stdlib.h>
    
        #define NUM_MAX_SIZE 20
    
        int** get2DArray( int rows, int columns )
        {
            int index = 0, **array = (int**)calloc( rows, sizeof( int* ) );
    
            for( ; index < rows ; index++ )
            {
                array[ index ] = (int*)calloc( columns, sizeof( int ) );
            }
    
            return array;
        }
    
    
        void print2DArray( int** matrix, int rows, int columns )
        {   
            int x = 0, y = 0;
            for( ; x < rows ; x++)
            {
                for( y = 0 ; y < columns ; y++)
                {
                    printf( "%d\t", matrix[x][y] );
                }
                puts("");
            }
        }
    
        void freeMatrix( int** matrix, int rows )
        {
            rows--;
            for( ; rows > -1 ; rows-- )
            {
                free( matrix[ rows ] );
            }
    
            free( matrix );
        }
    
        int main()
        {
            FILE* file = fopen( "input.txt", "r" );
    
            int rows, columns;
    
            fscanf( file, "%d", &rows );
            fscanf( file, "%d", &columns );
    
            int x = 0, y = 0, num , **matrix = get2DArray( rows, columns );
    
            for( ; x < rows ; x++)
            {
                for( y = 0 ; y < columns ; y++)
                {
                    fscanf( file, "%d", &matrix[ x ][ y ] );
                }
            }
    
            print2DArray( matrix, rows, columns );
            freeMatrix( matrix, rows );
    
            fclose( file );
            return 0;
        }
    

    【讨论】:

      猜你喜欢
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      相关资源
      最近更新 更多