1
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::wstringstream instream;
    // Load the simulated `instream` using a UTF-8 string literal [1]
    instream << L"█\n \n▀\n▄\n▓\n";

    // Print entire `instream`
    std::wcout << instream.rdbuf();
}

It doesn't print anything.

Also, I have tried other versions, but I can't print more than 256 characters in ASCII.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I had a similar problem, for me `#include`ing `` and putting `std::setlocale(LC_ALL, "");` at the start of `main` worked. – mediocrevegetable1 Aug 10 '21 at 15:23
  • 1
    BTW I don't think that's a UTF-8 string literal, instead it's a wide string literal that I think is probably UTF-16 or UTF-32 depending on `sizeof (std::wchar_t)` but I'm not completely certain. – mediocrevegetable1 Aug 10 '21 at 15:26
  • Is there anything else? Your advice didn't help me... – Peter Nasonov Aug 10 '21 at 15:32
  • Yeah. Right that's a wchar_t. (i checked - sizeof(wchar_t) is 2 bytes). But when i do something like this: `#include #include #include #include int main() { std::setlocale(LC_ALL, ""); for (wchar_t a = 1; a > 0; a+= 1) std::wcout << (int)a << L") |" << a << L"| "; }` only 128 characters are output. But 2^16 / 2 is not 128 in any way. – Peter Nasonov Aug 10 '21 at 15:34
  • Not really, sorry. I don't really fiddle around with Unicode too much :\ – mediocrevegetable1 Aug 10 '21 at 15:34
  • Not problem, @mediocrevegetable1 – Peter Nasonov Aug 10 '21 at 15:37
  • @PeterNasonov Have you tried doing `std::locale::global(std::locale("")); std::wcout.imbue(std::locale());` after calling `std::setlocale`? AFAIK, `std::setlocale` doesn't affect C++'s input and output streams. – Ruks Aug 10 '21 at 15:40
  • @Ruks,I tried it now. But it didn't help) – Peter Nasonov Aug 10 '21 at 15:46
  • Also, what is the encoding of the source file and the code page of the console (assuming you are on recent Win10 version that do support UTF-8 code page)? – Phil1970 Aug 10 '21 at 17:07
  • @PeterNasonov "*But when i do something like this: ... only 128 characters are output*" - [works fine for me](https://ideone.com/0gkGZf) (until it crashes with a runtime error, but I think that is a limitation of the site, not the code). Though, `a > 0` is assuming `wchar_t` is a signed type and the loop will overflow to a zero/negative value, but that is [not guaranteed](https://stackoverflow.com/a/11953419/65863) (signed overflow is undefined behavior). You should use `a < std::numeric_limits::max()` instead. – Remy Lebeau Aug 10 '21 at 17:33
  • 1
    It depends on your compiler and OS. – n. m. could be an AI Aug 10 '21 at 17:35
  • It does depend on OS. For example, if Windows see https://stackoverflow.com/a/47501816/235698. – Mark Tolonen Aug 10 '21 at 20:24

0 Answers0