Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7034849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:14:06+00:00 2026-05-28T01:14:06+00:00

void matrix_writebinary(struct matrep *mat) { int i,j; FILE *fptr; if((fptr=fopen(matrixA.bin,wb))==NULL) { puts(Cannot Open File);

  • 0
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
                         }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:14:07+00:00Added an answer on May 28, 2026 at 1:14 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

void addNewNode (struct node *head, int n) { struct node* temp = (struct node*)
void FileManager::CloseFile(File * const file) { for (int i = 0; i < MAX_OPEN_FILES;
void (int a[]) { a[5] = 3; // this is wrong? } Can I
void some_func(int param = get_default_param_value());
void foo(void **Pointer); int main () { int *IntPtr; foo(&((void*)IntPtr)); } Why do I
void BinaryTree::InitializeFromFile(string Filename){ ifstream inFile; treenode* Freq[256]; inFile.open(Filename.c_str(), fstream::binary); if(inFile.fail()){ cout<<Error in opening file
Let's say I have the following: public void MakeMatrix(int matrixLength) { int[,] Matrix =
I am reading a matrix from a file and have the following code: int
Consider following codes: #include <stdio.h> #include <malloc.h> void allocateMatrix(int **m, int l, int c)
Trying to run this piece of code: void print_matrix(matrix* arg) { int i, j;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.