C matrix fread (), read in nine bad values ​​regardless of the size of the matrix

advertisements
void matrix_writebinary(struct matrep *mat) {
 int i,j;

FILE *fptr;
if((fptr=fopen("matrixA.bin","wb"))==NULL)
{
puts("Cannot Open File");
}

fwrite(&mat->rows,sizeof(int),1,fptr);
fwrite(&mat->cols,sizeof(int),1,fptr);
fwrite(mat->matrix,sizeof(double),(mat->rows*mat->cols),fptr);

fclose(fptr);
}
/***********************************************************/

double *matrix_readbinary(struct matrep *mat) {
int i,j;

FILE *fptr;
if((fptr=fopen("matrixA.bin","rb"))==NULL)
{
puts("Cannot Open File");
}

fread(&mat->rows,sizeof(int),1,fptr);
fread(&mat->cols,sizeof(int),1,fptr);

mat->matrix = (double *) malloc( sizeof( double ) * (mat->rows*mat->cols) ) ;
fread(mat->matrix,sizeof(double),(mat->rows*mat->cols),fptr);

fclose(fptr);
return mat->matrix;
}

I am using the above two functions to write a matrix to a binary file, and then read the matrix back from the file using fread.

Then I am printing the matrix, but every time I do, the first 9 elements of the matrix are zero, no matter what size mat.rows or mat.cols are, always 9 empty elements or -1.#R when trying to print, so I think that fread(val,sizeof(double),(mat->rows*mat->cols),fptr);

is reading in 9 bad doubles but I have no idea why...

In the main(), the user specifies the row and col size etc.

If someone could explain why I would really appreciate it.

Here is the func used to print

void print_matrix( struct matrep *mat )
{
double *ptr ;
int i, j ;

ptr=mat->matrix;

if ( mat->matrix==0 || mat->rows==0 || mat->cols==0 )
   {
   printf("\n\nEmpty matrix" );
   return ;
   }

printf( "\n\nrows %d, columns %d\n\n",  mat->rows,  mat->cols) ;
for ( i=0; i <  mat->rows; i++ )
    {
    for ( j=0; j <  mat->cols; j++ )
        {
        printf( "%5.2lf\t", *ptr++ );
        }
    printf( "\n" ) ;
}

}

and the structure used...

struct matrep {
  int rows,cols;
  double *matrix;
};
struct matrep MATRIX,MATRIX1;

code that call matrix_readbinary() in main()

//BINARY WRITE/READ
printf("\nWriting binary data now...\n");
matrix_writebinary( &MATRIX ) ;
_flushall();
getchar();
printf("\nReading binary data now...\n");
MATRIX1.matrix=matrix_readbinary(&MATRIX1 ) ; // reads above matrix from file as a copy
if(MATRIX1.matrix!=NULL){
                        printf("\nPrinting read .bin...\n");
                        print_matrix(&MATRIX1 ) ; // prints this copyed matrix
                         }


I created a test app under Windows with Visual Studio using your posted code and it worked fine.

#include <stdio.h>
#include <tchar.h>
#include <string.h>
#include <stdlib.h>

struct matrep
{
  int rows,cols;
  double *matrix;
};

void matrix_writebinary(struct matrep *mat)
{
  int i,j;
  FILE *fptr;
  if((fptr=fopen("matrixA.bin","wb"))==NULL)
  {
    puts("Cannot Open File");
  }
  fwrite(&mat->rows,sizeof(int),1,fptr);
  fwrite(&mat->cols,sizeof(int),1,fptr);
  fwrite(mat->matrix,sizeof(double),(mat->rows*mat->cols),fptr);
  fclose(fptr);
}

/***********************************************************/
double *matrix_readbinary(struct matrep *mat)
{
  int i,j;
  FILE *fptr;
  if((fptr=fopen("matrixA.bin","rb"))==NULL)
  {
    puts("Cannot Open File");
  }
  fread(&mat->rows,sizeof(int),1,fptr);
  fread(&mat->cols,sizeof(int),1,fptr);
  mat->matrix = (double*) malloc(sizeof(double)*(mat->rows*mat->cols));
  fread(mat->matrix,sizeof(double),(mat->rows*mat->cols),fptr);
  fclose(fptr);
  return mat->matrix;
} 

void print_matrix( struct matrep *mat )
{
  double *ptr ;
  int i, j ;
  ptr=mat->matrix;
  if ( mat->matrix==0 || mat->rows==0 || mat->cols==0 )
  {
    printf("\n\nEmpty matrix" );
    return ;
  }
  printf( "\n\nrows %d, columns %d\n\n",  mat->rows,  mat->cols) ;
  for ( i=0; i <  mat->rows; i++ )
  {
    for ( j=0; j <  mat->cols; j++ )
    {
      printf( "%5.2lf\t", *ptr++ );
    }
    printf( "\n" ) ;
  }
}

struct matrep MATRIX,MATRIX1;

int _tmain(int argc, _TCHAR* argv[])
{
  int  r;
  int c;
  double*  pdbl;

  MATRIX.rows = 4;
  MATRIX.cols = 3;
  pdbl = (double*) malloc(sizeof(double) * MATRIX.rows * MATRIX.cols);
  MATRIX.matrix = pdbl;
  for ( r = 0; r < MATRIX.rows; ++r )
  {
    for ( c = 0; c < MATRIX.cols; ++c )
    {
      *pdbl++ = c + r;
    }
  }
  matrix_writebinary(&MATRIX);
  print_matrix(&MATRIX);
  matrix_readbinary(&MATRIX1);
  print_matrix(&MATRIX1);
}