title: C++ Read file from disk and write it into shared memory
body: <p>My goal is to achieve the following:</p>
  
  <p>I want to read a file from disk (let's say it's an image file) and to write it into shared memory so I can read it from the shared memory from another process.
  First I followed <a href="https://msdn.microsoft.com/de-de/library/windows/desktop/aa366551(v=vs.85).aspx" rel="nofollow noreferrer">this msdn tutorial</a> to create a simple shared memory implementation to contain a string. It works fine.</p>
  
  <p>Then I found a way to read the image from disk. The implementation is as follows:</p>
  
  <pre><code>std::ifstream fin("path/to/img.png", std::ios::in | std::ios::binary);
  std::ostringstream oss;
  oss &lt;&lt; fin.rdbuf();
  std::string data(oss.str());
  </code></pre>
  
  <p>So now I have a <code>std::string</code> containing my data The <code>data.length()</code> indicates that the file I've read is successfully stored in there. In the msdn example, the type of the <code>MapViewOfFile</code> result is <code>LPTSTR</code>, so I looked for a way to cast the <code>std::string</code> I have to <code>LPTSTR</code>, which is as far I understand a <code>const wchar_t*</code>. I do this as follows:</p>
  
  <pre><code>std::wstring widestr = std::wstring(data.begin(), data.end());
  const wchar_t* widecstr = widestr.c_str();
  </code></pre>
  
  <p>But if I now check <code>_tcslen(widecstr)</code> the result is <code>4</code>. So I guess what I tried to do doesn't work. I also found this quote on another SO question:</p>
  
  <blockquote>
    <p>Notice: A std::string is suitable for holding a 'binary' buffer, where a std::wstring is not!</p>
  </blockquote>
  
  <p>(<a href="https://stackoverflow.com/questions/402283/stdwstring-vs-stdstring">Source</a>) This makes it sound like I can't store the file data the way I tried.</p>
  
  <p>So my question is: Did I just did a mistake somewhere or is my approach flawed? Maybe I need to use another file type for the result of <code>MapViewOfFile</code>? Maybe I need to initialy load the file into another type?</p>