title: Write Unicode strings into a file
body: <p>I'm trying to work out a class reading &amp; writing files. for strings there is two ways of it: ANSI and Unicode. ANSI functions' alright but something wrong with my Unicode ones.</p>
  
  <p>it's a bit wired that I can read Unicode files just fine, directly I mean, without checking or skipping "0xFEFF" stuff. it works no matter what language I'm in (I tried English, Chinese and Japanese). is there any thing I should know about?</p>
  
  <p>then the biggest problem jumped out: to write Unicode strings into a file. first I tried simple English as alphabet without '\n' character, it did works great. then i push '\n' into and things start going wrong: the output is inserted with many spaces as "a b c d e f g \n h i j k l m n \n o p q r s t \n u v w x y z " ('\n' works but so many spaces) and the file is ANSI again. don't ask characters in other languages, I can't even read them at all.</p>
  
  <p>so here is the question: what should I do to write Unicode strings into a file correctly and how? don't mention "_wopen" function please, the file is already opened with "fopen" function.</p>
  
  <p>answers &amp; advises would be so appreciated.</p>
  
  <p>I'm using Windows 7 and visual studio.</p>
  
  <p>Edit: it works for non-English characters with following code but still wrong with '\n'.</p>
  
  <pre><code>char* cStart = "\xff\xfe";
  
  if (::ftell(m_pFile) == 0)
      ::fwrite(cStart, sizeof(wchar_t), 1, m_pFile);
  </code></pre>
  
  <p>but how that works? I mean I didn't see it while I was reading the file.</p>
  
  <p>Edit: part of my code.</p>
  
  <pre><code>void File::ReadWText(wchar_t* pString, uint32 uLength)
  {
      wchar_t cLetter = L'\0';
      uint32 uIndex = 0;
  
      do {
          cLetter = L'\0';
          ::fread(&amp;cLetter, sizeof(wchar_t), 1, m_pFile);
          pString[uIndex] = cLetter;
      }while (cLetter != L'\0' &amp;&amp; !::feof(m_pFile) &amp;&amp; uIndex++ &lt; uLength);
      pString[uIndex] = L'\0';
  }
  
  void File::WriteWText(wchar_t* pString, uint32 uLength)
  {
      char* pStart = "\xff\xfe";
  
      if (::ftell(m_pFile) == 0)
          ::fwrite(pStart, sizeof(wchar_t), 1, m_pFile);
  
      m_uSize += sizeof(wchar_t) * ::fwrite(pString, sizeof(wchar_t), uLength, m_pFile);
  }
  
  void main()
  {
      ::File* pFile = new File();
      wchar_t* pWString = L"abcdefg\nhijklmn\nopqrst\nuvwxyz";
  
      pFile-&gt;Open("TextW.txt", File::Output);
      // fopen("TextW.txt", "w");
      pFile-&gt;WriteWText(pWString, ::wcslen(pWString));
      pFile-&gt;Close();
  }
  </code></pre>
  
  <p>output file's content is: "abcdefg਍栀椀樀欀氀洀渀ഀopqrst਍甀瘀眀砀礀稀", file's in Unicode.</p>
  
  <p>I don't know whether it's the right expression of "L'\n'", I've never worked with Unicode before.
  thanks for helping me :)</p>