title: Reading Sparse Matrix from Binary File in C
body: <p>Firstly, I am sorry for my English.</p>
  
  <p>I am trying to read an integer array which represents some sparse matrix from a binary and I have to print this matrix. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define MAX 20 
  //on this way i have written the matrix in the binary
  typedef struct{
      int lin,col;
      double val;
  }termen;
  typedef struct{
      int nl,nc,nn;
      termen* term;
  }sparse_matrix;
  
  void afisare_mat(sparse_matrix m){
  int i,j,k = 0;
  for(i = 0; i &lt; m.nl; i++){
      for(j = 0; j &lt; m.nc; j++){
          if(i == m.term[k].lin &amp;&amp; j == m.term[k].col){
              printf("%3.2lf ",m.term[k].val);
              k++;
          }else{
              printf("%d ",0);
          }
      }printf("\n");
     }
  }
  int main(){
      /*this is the code used for write in the binary file
      //i tried to print these values and seems to be ok
      sparse_matrix m1,m2,m3;
      m1.term = malloc(2*sizeof(termen));
      m1.nl = 3;
      m1.nc = 3;
      m1.nn = 2;
      m1.term[0].lin = 0;
      m1.term[0].col = 1;
      m1.term[0].val = 3.2;
      m1.term[1].lin = 1;
      m1.term[1].col = 2;
      m1.term[1].val = 5.6;
      afisare_mat(m1);
      m2.term = malloc(3*sizeof(termen));
      m2.nl = 4;
      m2.nc = 4;
      m2.nn = 3;
      m2.term[0].lin = 0;
      m2.term[0].col = 0;
      m2.term[0].val = 1.9;
      m2.term[1].lin = 1;
      m2.term[1].col = 2;
      m2.term[1].val = 0.5;
      m2.term[2].lin = 2;
      m2.term[2].col = 3;
      m2.term[2].val = 6.960;
      afisare_mat(m2);
      m3.term = malloc(1*sizeof(termen));
      m3.nl = 3;
      m3.nc = 3;
      m3.nn = 1;
      m3.term[0].lin = 1;
      m3.term[0].col = 2;
      m3.term[0].val = 6.75;7
      afisare_mat(m3);
      FILE *f = fopen("in.bin","wb");
      fwrite(&amp;m1,sizeof(sparse_matrix),1,f);
      fwrite(&amp;m2,sizeof(sparse_matrix),1,f);
      fwrite(&amp;m3,sizeof(sparse_matrix),1,f);
      fclose(f);
      */
      FILE *f = fopen("in.bin","rb");
      int n, v[10], i, j, k,nr = 0,it; //n=number of matrix asked, v=array with  
      //number(s)(&lt;= n) of matrix which have to print
      scanf("%d",&amp;n);
      sparse_matrix m;
      while(fread(&amp;k,sizeof(int),1,f) &gt; 0){
          fseek(f,2*sizeof(int),SEEK_CUR);
          fseek(f,sizeof(termen*),SEEK_CUR);
          nr++;
      }
      for(i = 0; i &lt; n; i++){
          scanf("%d",&amp;v[i]);
          if(v[i] &gt; nr)
              printf("That matrix not exist\n");
      }
  
      fseek(f,0,0);
      j = 0;
      while(fread(&amp;k,sizeof(int),1,f) &gt; 0){   //citesc nl din fiecare matrice
          for(i = 0; i &lt; n; i++){
              if(j == v[i]){  //number of matrix which i read;
                  //print matrix
  
                  m.nl = k;
                  fread(&amp;m.nc,sizeof(int),1,f);
                  fread(&amp;m.nn,sizeof(int),1,f);
                  m.term = malloc(m.nn*sizeof(termen));
                  fread(m.term,sizeof(termen),m.nn,f);
                  afisare_mat(m);
                  free(m.term);
                  fseek(f,(j+1)*sizeof(sparse_matrix),0);
              }
          }j++;
      }
      fclose(f);
      return 0;
  }
  </code></pre>
  
  <p>This code prints on my screen the form of matrix but full of zeroes. I think that the rest of the elements (!= 0) aren't read ok from the binary file. Can you help me, please?</p>