Endl does not work with wstring (unicode)

Here is the code:

  std::ofstream f("file1.txt");
  f<<"123"<<std::endl<<"456";           //(*1)

  /*std::stringstream ordinary_strstream; This works too
  ordinary_strstream<<"123"<<'\n'<<"456";
  f<<ordinary_strstream.str();*/

  std::wstringstream s;
  s<<L"123"<<std::endl<<L"456";         //(*2)
  s<<L"123"<<L"\n"<<L"456";             //(*3) 
  s<<"123"<<WCHAR(13)<<WCHAR(10)<<"456";//(*4)

  HANDLE h =CreateFileW(L"file2.txt", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

  ULONG l;
  WriteFile(h, s.str().c_str(), s.str().length() * 2, &l, NULL);

In the case of (* 1) there is a newline, in (* 2) and (* 3) I do not see a newline in file2.txt. There is a new line in (* 3). I use notepad.exeto view. There is no byte in the hex editor 0x0D, only 0x0A.

How to correctly put a new line in a text file in Unicode? Thank.

+3
source share
2 answers

: std::endl L'\n' char ( , ), L"\r\n". L"\n". WriteFile - , wstringstream ( , do (*1)). . , .

+9

. std::endl , \n (char wchar, ).

CreateFile ; .

, CreateFile, endl (0x0A).

, CR + LF.

+2

All Articles