title: C++ ifstream.read; reads bytes less then given n bytes
body: <p>I'm trying to split up data into little packets. I'm not exactly sure how this read method is suppose to work but I've given a buffer size of 512 to read from the file.
  But instead of getting 512 i just get 5 in my first packet. Others vary from 0 to above 512 ( which shouldn't happen).</p>
  
  <p>It's a zip file I'm trying to split up:
  In text the first few bytes look like this</p>
  
  <p><img src="https://i.stack.imgur.com/5EPIL.png" alt="text"><br>
  (de bucket like characters are actually 2 characters)           </p>
  
  <p>It seems to grab the first 5 bytes as it should but afterwards just stops and goes to the next read block.</p>
  
  <p>Since it's a buffer of 512 everything after the first 5 bytes is garbage.
  I'm using an ifstream. And the mode is set to Binary.</p>
  
  <p>Any suggestions?</p>
  
  <pre><code>void FileProcessor::send()
  {
      //If no file is opened return
      if(!_file.is_open()) return;
      //Reset position to beginning
      _file.seekg(0, ios::beg);
  
      //Result buffer
      char * buffer;
      char * partBytes = new char[_bufferSize];
  
      //Read the file and send it over the network
      while (_file.read(partBytes, _bufferSize))
      {
  
          buffer = Packet::create(Packet::FILE,partBytes);
          Packet *p = Packet::create(buffer);
          //cout &lt;&lt;  strlen(partBytes);
          //p-&gt;PrintHex(buffer,_bufferSize+Packet::HeaderSize);
  
          //break;
          cout &lt;&lt; "Normal size : \t" &lt;&lt; strlen(partBytes)&lt;&lt; "\tPacketSize: \t" &lt;&lt; p-&gt;getLength()&lt;&lt;"\n";
          //cout &lt;&lt; strcmp(p-&gt;getData().c_str(),partBytes) &lt;&lt; "\n";
          writeToFile(p-&gt;getData().c_str(),p-&gt;getData().length());
          delete p;
      }
      //Write final bytes if any
      if(_file.gcount())
      {
          //writeToFile(partBytes, _file.gcount());
  
  
          buffer = Packet::create(Packet::FILE,partBytes);
          Packet *p = Packet::create(buffer);
  
          writeToFile(p-&gt;getData().c_str(),p-&gt;getData().length());
          //cout &lt;&lt; p-&gt;getLength() &lt;&lt; "\n";
          delete p;
  
      }   
  
      //cout&lt;&lt; *p &lt;&lt; "\n";
      delete [] partBytes;
  }
  </code></pre>
  
  <p>Im just testing a direct read write right now.</p>