-1

How do I store Unicode characters in a text file in separate lines using C++? I tried using \n and \r; also with their hex values. But, in the .txt file \n and \r are read as garbage values.

Here is the code snippet:

{
    std::wofstream wof;
    wof.imbue(std::locale(std::locale::empty(), new std::codecvt_utf16<wchar_t, 
    0x10ffff, std::generate_header>));
    wof.open(L"file3.txt");
    wof << L"Test.\x263a \x263c \x263d \x263e A:B:C, 我一个人来  人人都爱喝可乐。 " << endl;
    wof << L"\n  Another test. \x002c \x263a \263e" << endl;
    wof << L"\n  我一个人来。      一个人     我一个人来  人人都爱喝可乐。" << endl;
    wof << L" Test \x263a \x263c \x263d \x263e 我一个人来  人人都爱喝可乐。 " << endl;
    wof << L"\n 我一个人来  人人都爱喝可乐。" << endl;
}
Azeem
  • 11,148
  • 4
  • 27
  • 40
Khushboo
  • 43
  • 2
  • 9
  • [don't use `endl`, unless you really know and need what it's doing](https://stackoverflow.com/q/213907/995714). Use `L"\n"` or `L'\n'` instead – phuclv Jan 16 '18 at 10:54
  • [endl doesn't work with wstring (unicode)](https://stackoverflow.com/q/10363564/995714). [*In many implementations, standard output is line-buffered, and writing '\n' causes a flush anyway, unless std::ios::sync_with_stdio(false) was executed. In those situations, unnecessary endl only degrades the performance of file output, not standard output*](http://en.cppreference.com/w/cpp/io/manip/endl) – phuclv Jan 16 '18 at 10:59
  • I have tried it but still not getting the output in the next line that is being stored in the .txt file – Khushboo Jan 16 '18 at 17:47
  • Use `L"\r\n"` for endl – Barmak Shemirani Jan 17 '18 at 01:15
  • I tried that but it doesn't work. I tried to put hex values of \r\n too, but that is also not working. – Khushboo Jan 17 '18 at 05:01

1 Answers1

0

Open the file in binary as shown and add BOM marker L"\xFEFF" to indicate the file is UTF-16 (LE) mode. With this method you need pubsetbuf

Use L"\r\n" for end of line

std::wofstream wof(L"file3.txt", std::ios::binary);
if(wof)
{
    wchar_t buf[128];
    wof.rdbuf()->pubsetbuf(buf, 128);

    wof << L"\xFEFF";

    wof << L"Test.\x263a \x263c \x263d \x263e A:B:C, 我一个人来  人人都爱喝可乐。\r\n";
    wof << L"Another test. \x002c \x263a \263e\r\n";
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77