title: Read/Write to PPM Image File C++
body: <p>Trying to read and write to/from a PPM Image file (.ppm) in the only way I know how:</p>
  
  <pre><code>std::istream&amp; operator &gt;&gt;(std::istream &amp;inputStream, PPMObject &amp;other)
  {
      inputStream.seekg(0, ios::end);
      int size = inputStream.tellg();
      inputStream.seekg(0, ios::beg);
  
      other.m_Ptr = new char[size];
  
  
      while (inputStream &gt;&gt; other.m_Ptr &gt;&gt; other.width &gt;&gt; other.height &gt;&gt; other.maxColVal)
      {
          other.magicNum = (string) other.m_Ptr;
      }
  
      return inputStream;
  }
  </code></pre>
  
  <p>My values correspond to the actual file. So I cheerfully attempt to write the data:</p>
  
  <pre><code>std::ostream&amp; operator &lt;&lt;(std::ostream &amp;outputStream, const PPMObject &amp;other)
  {
      outputStream &lt;&lt; "P6"     &lt;&lt; " "
          &lt;&lt; other.width       &lt;&lt; " "
          &lt;&lt; other.height      &lt;&lt; " "
          &lt;&lt; other.maxColVal   &lt;&lt; " "
         ;
  
      outputStream &lt;&lt; other.m_Ptr;
  
      return outputStream;
  }
  </code></pre>
  
  <p>I am making sure to open the file using std::ios::binary for both reading and writing:</p>
  
  <pre><code>int main ()
  {
      PPMObject ppmObject = PPMObject();
      std::ifstream image;
      std::ofstream outFile;
  
      image.open("C:\\Desktop\\PPMImage.ppm", std::ios::binary);
      image &gt;&gt; ppmObject;
  
      image.clear();
      image.close();
  
      outFile.open("C:\\Desktop\\NewImage.ppm", std::ios::binary);
      outFile &lt;&lt; ppmObject;
  
      outFile.clear();
      outFile.close();
  
      return 0;
  }
  </code></pre>
  
  <p>Logic Error:</p>
  
  <p>I am only writing a portion of the image. There is no problem with the header or opening the file manually.</p>
  
  <p>Class public member variables:</p>
  
  <p>The m_Ptr member variable is a char * and height, width maxColrVal are all integers.</p>
  
  <p>Attempted Solution:</p>
  
  <p>Using inputStream.read and outputStream.write to read and write data but I don't know how and what I have tried doesn't work.</p>
  
  <p>Since my char * m_Ptr contains all of the pixel data. I can iterate through it:</p>
  
  <pre><code>for (int I = 0; I &lt; other.width * other.height; I++) outputStream &lt;&lt; other.m_Ptr[I];
  </code></pre>
  
  <p>But this causes a run-time error for some reason..</p>