2
#include <iostream>
#include <locale>

int main(int argc, char** argv) {
    std::wcout.imbue(std::locale("zh_CN.UTF-8"));

    std::wcout << wchar_t(0) << L"哈哈" << std::endl;
    std::cout << char(0) << "haha" << std::endl;
    std::cout << "---------------" << std::endl;

    std::wcout.clear();
    std::wcout << L"哈哈" << std::endl;
    std::cout << "haha" << std::endl;
    std::cout << "---------------" << std::endl;

    std::wcout << L'\0' << L"哈哈" << std::endl;
    std::cout << '\0' << "haha" << std::endl;
    std::cout << "---------------" << std::endl;

    std::wcout.clear();
    std::wcout << L"哈哈" << std::endl;
    std::cout << "haha" << std::endl;

    return 0;
}

The wchar_t(0) and L'\0' seem to different from char(0) and '\0' and cause the ostream to have bad state.

It took me some time to figure out the missing output is not caused by the locale setting but the wchar_t since my original program has somewhere output a wchar_t(0) or '\0'.

My question is how are they different from the char version? And how to correctly use an empty wchar_t?

Thanks in advance.

Saddle Point
  • 3,074
  • 4
  • 23
  • 33
  • I believe it's `L'\0'`. – iBug May 16 '18 at 04:01
  • Text streams do not support outputting a null character, so you should not try this in the first place – M.M May 16 '18 at 04:04
  • @M.M Thanks for your information. I come from Python and don't know about this in C++. Is there any reference to this? – Saddle Point May 16 '18 at 04:11
  • You can try using the `put` function to write a single character, no formatting. But in general for binary data you should not be using C++ streams. – Ben Voigt May 16 '18 at 04:18

1 Answers1

3

The null wide character can be written as wchar_t(0) or L'\0'.

The different behaviour you observe is because cout and wcout are text streams. According to cppreference, you should only use printable characters, \t and \n on a text stream. Sending a null character to the text stream may have unexpected results.

If you want to use cout as a binary stream in Windows there are some hacks you can do , see here for ideas.

I am not sure whether those hacks would work for wcout; but from past experience, the status of wcout support in compilers is dubious and I have found it more reliable to just use stdout and do any required translation using facets or explicit functions etc.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Thank you very much. I'm implementing a `trie` tree and giving an empty wchar_t for the default root node. I can get the correct output of the whole tree recursively unless the root is not an empty wchar_t. I think I should change this default behavior now. – Saddle Point May 16 '18 at 04:23