endl doesn't 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 (*1) case there is a newline, in the (*2) and (*3) i see no newline in the file2.txt. In the (*3) there is a newline. I use notepad.exe for browsing. Hex editor shows no 0x0D byte, only 0x0A.

How should i properly put the newline in unicode text file? Thank you.


That's expected: std::endl writes a L'\n' to a wide-char stream (and flushes it, but it's not relevant here), never L"\r\n". It's even more obvious when writing a literal L"\n". WriteFile doesn't perform any kind of newline substitution, neither does wstringstream (note that file streams <strong>do</strong> perform newline substitution when opened in text mode, and that's why you get a proper newline in (*1)). Therefore, in the file there will be no carriage return characters. If you want them, write them explicitely.


Stringstreams don't do line-ending conversion. std::endl is exactly the same as \n (char or wchar, depending on context).

CreateFile doesn't do line-ending conversion; it's strictly binary.

So when you create a string with a stringstream and write it to a file with CreateFile, an endl will just be a newline (0x0A) character.

Notepad doesn't cope well with files whose line endings are just newlines instead of CR+LF.


  • How to write incrementally to a text file and flush output
  • Invalid index for this SqlParameterCollection with Count
  • How to use StreamWriter.WriteAsync and catch exceptions?
  • How to append a file with a newline using %%writefile -a command in jupyter?
  • Different types evaluated for keyword var
  • c++ - properly writing std::string to binary file
  • Passing std::vector as array of C-style strings (char**)
  • Timer with TimeSpan and self-made timer are slower than Stopwatch
  • C/C++ HDF5 Read string attribute
  • MySQL C++ Connector: Get the insert_id
  • why is the sort() not working?
  • Sending a C# string as an argument to a unmanaged C++ DLL function
  • C/C++ System portable way to change maximum number of open files
  • Random(staticSeed).Next() alternative that will never change implementation and is guaranteed consis
  • TextPad “find in files” not matching on simple OR regex
  • Standard way for writing a debug mode in C++
  • Complex python3 csv scraper
  • C++ Text File Reading
  • Where are files stored on an Android emulator?
  • Printing out Japanese (Chinese) characters
  • VBScript to check for open process by user
  • subprocess.Popen('start') fails
  • aapt.exe'' finished with non-zero exit value 1
  • SAXReader not re-ecape characters
  • Tamper-proof configuration files in .NET?
  • Code in Job's Script Block after Start-Process Does not Execute
  • AES padding and writing the ciphertext to a disk file
  • Running a C# exe file
  • How to make Safari send if-modified-since header?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • 0x202A in filename: Why?
  • How to pass list parameters for each object using Spring MVC?
  • Circular dependency while pushing http interceptor
  • retrieve vertices with no linked edge in arangodb
  • Linker errors when using intrinsic function via function pointer
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • apache spark aggregate function using min value
  • JaxB to read class hierarchy