title: Cannot get integer from stringstream in C++
body: <p>I just searching for the stream (memory stream or file stream) then I found fstream and stringstream</p>
  
  <p>But, I have tried both but it always giving me wrong output when get the integer, I am using binary file for testing this (the data isn't string, the data is bunch of bytes), here is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  
  using namespace std;
  void getinfo(char* buffer, int length)
  {
      stringstream stream(ios::in | ios::out | ios::binary);
      // write data to stream
      stream.write(buffer, length);
  
      // reset position
      stream.seekg(0);
  
      // get 4 bytes (value is OMC with null-byte at the end)
      char* format = new char[4];
      stream &gt;&gt; format;
  
      // get 4 byte (value is depending of file version)
      int ojmver = 0; 
      stream.read(reinterpret_cast&lt;char*&gt;(&amp;ojmver), sizeof ojmver);
  
      // get 2 byte (encryption signature 1 that used for this file)
      short encsign1 = 0; 
      stream.read(reinterpret_cast&lt;char*&gt;(&amp;encsign1), sizeof encsign1);
  
      // get 2 byte (encryption signature 2 that used for this file)
      short encsign2 = 0;
      stream.read(reinterpret_cast&lt;char*&gt;(&amp;encsign2), sizeof encsign2);
  
      // get 4 byte (samples count)
      int samplecount = 0; 
      stream.read(reinterpret_cast&lt;char*&gt;(&amp;samplecount), sizeof samplecount);
  
      // show info
  
      cout &lt;&lt; "Format\t\t\t: " &lt;&lt; format;
      cout &lt;&lt; "\nOJM Ver\t\t\t: " &lt;&lt; ojmver;
      cout &lt;&lt; "\nEnc Sign1\t\t: " &lt;&lt; encsign1;
      cout &lt;&lt; "\nEnc Sign1\t\t: " &lt;&lt; encsign2;
      cout &lt;&lt; "\nSample Count\t\t: " &lt;&lt; samplecount;
      cout &lt;&lt; "\n\n";
  }
  
  int main()
  {
      fstream fs = fstream("D:\\o2ma100.ojm", ios::in | ios::out | ios::binary);
      fs.seekg(0, fs.end);
      int length = fs.tellg();
      fs.seekg(0, fs.beg);
  
      char* buffer = new char[length];
      fs.read(buffer, length);
  
      if (fs)
          cout &lt;&lt; "Length: " &lt;&lt; length &lt;&lt; "\nSuccess to load data\n";
      else
          cout &lt;&lt; "Failed to load data\n";
  
      fs.seekg(0, fs.beg);
  
      // get 4 bytes (value is OMC with null-byte at the end)
      char* format = new char[4];
      fs.read(format, 4);
  
      // get 4 byte (value is depending of file version)
      int ojmver = 0; 
      fs.read(reinterpret_cast&lt;char*&gt;(&amp;ojmver), sizeof ojmver);
  
      // get 2 byte (encryption signature 1 that used for this file)
      short encsign1 = 0; 
      fs.read(reinterpret_cast&lt;char*&gt;(&amp;encsign1), sizeof encsign1);
  
      // get 2 byte (encryption signature 2 that used for this file)
      short encsign2 = 0;
      fs.read(reinterpret_cast&lt;char*&gt;(&amp;encsign2), sizeof encsign2);
  
      // get 4 byte (samples count)
      int samplecount = 0; 
      fs.read(reinterpret_cast&lt;char*&gt;(&amp;samplecount), sizeof samplecount);
  
      cout &lt;&lt; "\nRead Data using fs:\n";
  
      cout &lt;&lt; "Format\t\t\t: " &lt;&lt; format;
      cout &lt;&lt; "\nOJM Ver\t\t\t: " &lt;&lt; ojmver;
      cout &lt;&lt; "\nEnc Sign1\t\t: " &lt;&lt; encsign1;
      cout &lt;&lt; "\nEnc Sign1\t\t: " &lt;&lt; encsign2;
      cout &lt;&lt; "\nSample Count\t\t: " &lt;&lt; samplecount;
  
      fs.close();
  
      cout &lt;&lt; "\n\nRead Data Using stringstream:\n";
      getinfo(buffer, length);
  
      system("PAUSE");
      return 0;
  }
  </code></pre>
  
  <p>It give me following result:</p>
  
  <pre><code>Length: 4231047 // -&gt; this correct :)
  Success to load data // -&gt; this correct :)
  
  Read Data using fs:
  Format                  : M30    // -&gt; this correct :)
  OJM Ver                 : 196608 // -&gt; this correct :)
  Enc Sign1               : 16     // -&gt; this correct :)
  Enc Sign1               : 0      // -&gt; this correct :)
  Sample Count            : 300    // -&gt; perfect :D
  
  Read Data Using stringstream:
  Format                  : M30         // -&gt; this correct :)
  OJM Ver                 : 50331648    // -&gt; this wrong :(
  Enc Sign1               : 4096        // -&gt; this wrong :(
  Enc Sign1               : 0           // -&gt; this wrong :(
  Sample Count            : 76800       // -&gt; this wrong :(
  
  Press any key to continue . . .
  </code></pre>
  
  <p>I was creating similar application in C#.NET, I have tested the file, and it give me correct output (using FileStream and MemoryStream in C# with .NET Framework 2.0), here the output:</p>
  
  <pre><code>Length: 4231047 // -&gt; Length is correct
  Success to load data
  
  Read Data using FileStream:
  Format                  : M30     // -&gt; this correct :)
  OJM Ver                 : 196608  // -&gt; this correct :)
  Enc Sign1               : 16      // -&gt; this correct :)
  Enc Sign1               : 0       // -&gt; this correct :)
  Sample Count            : 300     // -&gt; perfect :D
  
  Read Data Using MemoryStream:
  Format                  : M30     // -&gt; this correct :)
  OJM Ver                 : 196608  // -&gt; this correct :)
  Enc Sign1               : 16      // -&gt; this correct :)
  Enc Sign1               : 0       // -&gt; this correct :)
  Sample Count            : 300     // -&gt; perfect :D
  </code></pre>
  
  <p>I need the stream that handle the memory and file, so I need how to read data correctly with both stream...</p>
  
  <p>anyone can help me? I am new in C++
  Thanks :D</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I changed the code, Thanks to john, it worked for fstream, but it still not work for stringstream</p>