title: std::vector<unsigned char> remains empty after reading binary file using std::ifstream
body: <p>This is my first question on StackOverflow so I apologize in advance if there is anything I am missing in my question or some rule I didn't follow. Please edit my question or tell me in the comments how I should improve my question, thank you.</p>
  <p>I am trying to read a binary file into a <code>std::vector&lt;unsigned char&gt;</code> in C++ using <code>std::ifstream</code>. The problem is that it appears the file was read successfully, but the vector remains empty.</p>
  <p>Here is the function that I used for reading the file:</p>
  <pre><code>void readFile(const std::string &amp;fileName, std::vector&lt;unsigned char&gt; &amp;fileContent)
  {
      std::ifstream in(fileName, std::ifstream::binary);
  
      // reserve capacity
      in.seekg(0, std::ios::end);
      fileContent.reserve(in.tellg());
      in.clear();
      in.seekg(0, std::ios::beg);
  
      // read into vector
      in.read(reinterpret_cast&lt;char *&gt;(fileContent.data()), fileContent.capacity());
  
      if(in)
          std::cout &lt;&lt; &quot;all content read successfully: &quot; &lt;&lt; in.gcount() &lt;&lt; std::endl;
      else
          std::cout &lt;&lt; &quot;error: only &quot; &lt;&lt; in.gcount() &lt;&lt; &quot; could be read&quot; &lt;&lt; std::endl;
  
      in.close();
  }
  </code></pre>
  <p>And this is how I called the function in <code>main()</code>:</p>
  <pre><code>std::vector&lt;unsigned char&gt; buf;
  readFile(&quot;file.dat&quot;, buf);
  std::cout &lt;&lt; &quot;buf size: &quot; &lt;&lt; buf.size() &lt;&lt; std::endl;
  </code></pre>
  <p>When I run the code, I get the following output:</p>
  <pre><code>all content read successfully: 323
  buf size: 0
  </code></pre>
  <p>When I try to print the items in the vector like this:</p>
  <pre><code>for(const auto &amp;i : buf)
  {
      std::cout &lt;&lt; std::hex &lt;&lt; (int)i;
  }
  std::cout &lt;&lt; std::dec &lt;&lt; std::endl;
  </code></pre>
  <p>I get empty output.</p>
  <h3>Things I have checked</h3>
  <ul>
  <li><code>file.dat</code> is in the same directory as the program</li>
  <li><code>file.dat</code> is not empty</li>
  </ul>
  <p>So, is there anything I did wrong? Why is the vector empty after reading?</p>