title: Reading a .bmp image with fread()
body: <p>I am trying to read a .bmp image and write the data into a text file. Code is running fine but the problem is, it cannot read whole image once so I have to call <code>fread()</code> function many times. While doing this my code is repeatedly storing the first read data into the text file. What changes do I have to do in order to read the whole image properly? Below is my code snippet.</p>
  
  <pre><code>int size = width * height;
  unsigned char* data = new unsigned char[size]; 
  filename = "image.bmp";
  fname = "image_data.txt";
  FILE* f = fopen(filename, "rb");
  FILE *fp = fopen(fname, "w");
  
  while(totalBytes &lt; size)
  {
      readsize = fread(data, sizeof(unsigned char), size, f);
      totalBytes += readsize;
      for(i = 0; i &lt; readsize; i++)
      {
          fprintf(fp, "%d", data[i]);
          if((i % width) == 0 &amp;&amp; i != 0)
              fprintf(fp, "\n");
      }
      fseek(f, readsize, SEEK_SET);
      readsize = 0;
  }
  </code></pre>