将文件中的数字读入2D矩阵

时间:2016-02-05 09:29:46

标签: c matrix

我正在尝试编写一个函数来将文件中的数字读入2D矩阵; mm_alloc()函数是一个创建空矩阵的函数 该文件的第一行是矩阵的大小;但是代码似乎无法从文件中读取,我的问题是什么?我是否错误地使用了fgets()sscanf()

double **mm_alloc( int size )
{
    double **matrix;
    int i;
    matrix = (double **)malloc((size) * sizeof(double *));
    for ( i = 0; i < size; i++ ) {
        matrix[i] = (double *)malloc(size);
    }
    return( matrix );
}


double **mm_read( char *filename, int *size ) 
{
    FILE *myfile;
    myfile = fopen(filename, "r");
    char read[256];
    fgets(read, 256, myfile);
    sscanf(read, "%d", size);
    int i, j;
    double **Matrix;
    Matrix = mm_alloc( *size );
    for ( i = 0; i < *size; i++ ) {
        for ( j = 0; j < *size; j++) {
            fgets(read, 256, myfile);
            sscanf(read, "%lf", &Matrix[i][j]);
        }
    }
    fclose (myfile);
    return( Matrix );
}

void mm_free( int size, double **matrix  )
{
    int i;
    for ( i = 0; i < size; i++ ) {
        free(matrix[i]);
    }
    free(matrix);
}

void mm_print( int size, double **matrix )
{
    int i, j;
    for ( i = 0; i < size; i++ ) {
        for ( j = 0; j < size; j++ ) {
            printf("%16f", matrix[i][j]);
        }
        printf("\n");
    }
}

double **mm_swap( int size, double **matrix ) 
{
    double **swap;
    swap = mm_alloc(size);
    int i, j;
    for ( i = 0; i < size; i++ ) {
        for ( j = 0; j < (size-2); j++) {
            swap[i][j] = matrix[i][j+2];
            swap[i][j+2] = matrix[i][j];
        }
    }
    return( swap );
}

double **mm_matrix_mult( int size, double **matrix, double **transpose )
{
    double **matrix_mult;
    matrix_mult = mm_alloc(size);
    int i, j, k;
    double result;
    for ( i = 0; i < size; i++ ) {
        for ( j = 0; j < size; j++ ) {
            result = 0;
            for ( k = 0; k < size; k++ ) {
                result += (matrix[i][k] * transpose[k][j]);
            }
            matrix_mult[i][j] = result;
        }
    }
    return( matrix_mult );
}

int main()
{
    char filename[256];
    double **matrix=NULL;
    double **swap_matrix=NULL;
    double **results_matrix=NULL;
    int size=0;

    printf("Please enter the matrix data file name: ");
    scanf( "%s", filename );

    matrix = mm_read( filename, &size );
    swap_matrix = mm_swap( size, matrix ); 
    results_matrix = mm_matrix_mult( size, matrix, swap_matrix );

    mm_print( size, results_matrix );
    mm_free( size, matrix );
    mm_free( size, swap_matrix );
    mm_free( size, results_matrix );
    return( 0 ); 
}

文件内容:

4
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0
11.0
12.0
13.0
14.0
15.0
16.0

2 个答案:

答案 0 :(得分:2)

您正在使用&#34; w&#34;。

打开文件

这将删除该文件。您需要打开&#34; r&#34;从文件中读取。

答案 1 :(得分:1)

您的分配功能分配给很少的内存。

这一行

    matrix[i] = (double *)malloc(size);

至少应该

    matrix[i] = (double *)malloc(size * sizeof(double));

甚至更好地删除这个无用的演员:

    matrix[i] = malloc(size * sizeof(double));

此外,完全错过了分配功能来执行任何错误检查。 `malloc()可能会失败,具体取决于请求的内存大小。

下面请找一个安全的实现:

/* Allocates a pointer matrix to an array of size pointers
   each pointing to size doubles.

   Returns matrix or NULL on error. 
 */

double ** mm_alloc(size_t size)
{
  double ** matrix =  malloc(size * sizeof *matrix);
  if (NULL != matrix) 
  {
    for (size_t i = 0; i < size; ++i) 
    {
      matrix[i] = malloc(size * sizeof *matrix[i]);
      if (NULL == matrix[i])
      {  /* Clean up. */
        while (0 < i)
        {
          --i;
          free(matrix[i]);
        }

        free(matrix);
        matrix = NULL;

        break;
      }
    }
  }

  return matrix;
}