title: Need explanation of reading and writing of wchar_t* to binary file
body: <p>can someone explain me what is a proper way to write wchar_t* string to binary file and then read it back(using fread/fwrite)?</p>
  
  <p>here is what i have (its working)</p>
  
  <pre><code>struct someStruct{
      int someint;
      wchar_t* data;
      int someint2;
  }
  someStruct s;
  s.someint = 111;
  s.data = L"blah blah .... lets say 1-1000 characters long";
  s.someint2 = 222;
  
  //writing
  FILE * f = _wfopen(L"myfile",L"wb");
  fwrite(&amp;(s.someint), sizeof(int), 1, f);
  fwrite(&amp;(s.data), sizeof(wchar*), 1, f);
  fwrite(&amp;(s.someint2), sizeof(int), 1, f);
  fclose(f);
  
  //reading
  FILE * f2 = _wfopen(L"myfile",L"rb");
  fread(&amp;(s2.someint), sizeof(int), 1, f2);
  fread(&amp;(s2.data), sizeof(wchar*), 1, f2);
  fread(&amp;(s2.someint2), sizeof(int), 1, f2);
  fclose(f2);
  </code></pre>
  
  <p>everything is working, values are properly loaded. <strong>the question is what should be the second parameter of fread and fwrite in this specific example</strong>, why is it working with e.g. 4(which is sizeof(wchar_t*)) or 20 and causing buffer overrun with 150, these values vary based on data length, </p>
  
  <p>EDIT: these are what i have been using (was working last time i checked it :P 1 year ago):</p>
  
  <pre><code>wchar_t* loadStrFromFile(FILE* file) {
      int strLen;
      fread(&amp;(strLen), sizeof(int), 1, file);
      wchar_t* result = new wchar_t[strLen];
      fread(result, strLen, sizeof(wchar_t), file);
      return result;
  }
  
  void saveStrToFile(const wchar_t*&amp; data, FILE* file) {
      int strLen = wcslen(data)+1;
      fwrite(&amp;strLen, sizeof(int), 1, file);
      fwrite(data, strLen, sizeof(wchar_t), file);
  }
  </code></pre>