C 将非偶数行的文本文件读入二维数组

C 将非偶数行的文本文件读入二维数组,c,arrays,2d,C,Arrays,2d,很抱歉,我对C很陌生,我现在很难受。我有一个输入文本文件,内容如下: 5 3 383 386 287 415 293 335 368 492 149 421 362 27 190 59 263 我试着把它读入一个2D数组。我尝试的是: FILE * fin = NULL; fin = fopen("myTestData.txt", "r"); int twod[MAX_ROWS][MAX_COLS]; int i, j, num, row, col; fscanf(fin, "%d%d",

很抱歉,我对C很陌生,我现在很难受。我有一个输入文本文件,内容如下:

5 3
383 386 287
415 293 335
368 492 149
421 362 27
190 59 263
我试着把它读入一个2D数组。我尝试的是:

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;
    }
}
文件*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);
财务总监(财务);
fin=fopen(“myTestData.txt”、“r”);
对于(i=0;i
我的问题是,在第一行的空白处(TWOD〔0〕〔2〕),它分配第二行(383)的第一个整数的值。如何才能使[0][2]获得空值


感谢您的帮助

删除您关闭并重新打开文件的行。读入行数和列数后,只需处理剩余的数据,这些数据结构良好,大多数家庭作业问题也是如此

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");
}
文件*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);
//财务总监(财务);
//fin=fopen(“myTestData.txt”、“r”);
对于(i=0;i
#包括
#包括
#定义NUM\u MAX\u大小20
int**get2DArray(int行、int列)
{
int index=0,**数组=(int**)calloc(rows,sizeof(int*);
对于(;索引<行;索引++)
{
数组[index]=(int*)calloc(columns,sizeof(int));
}
返回数组;
}
void print2DArray(整数**矩阵、整数行、整数列)
{   
int x=0,y=0;
对于(;x<行;x++)
{
对于(y=0;y-1;行--)
{
自由(矩阵[行]);
}
自由(矩阵);
}
int main()
{
FILE*FILE=fopen(“input.txt”,“r”);
int行、列;
fscanf(文件“%d”行和行);
fscanf(文件“%d”列和列);
int x=0,y=0,num,**矩阵=get2DArray(行,列);
对于(;x<行;x++)
{
对于(y=0;y
您可以逐行读取输入,并将其标记为数字。如果数字较少,则为其余元素指定虚拟值。顺便说一下,第一行似乎指定了输入数据中的行数和列数。这就是代码使用它的方式(请参见
是如何赋值的)。因此,您只需从
for
循环的第二行开始读取。C中的int没有
null
值。
    #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;
    }