Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

The following piece of code produces a file with a single byte when compiled in MSVC, but when compiled by GCC 8.3 it produces an empty file.

std::wofstream out("tmpUTF16BrokenBOMFile.txt", std::ios_base::binary);
const wchar_t c = 0xFE;
// Both methods dont work:
// out.write(&c, 1);
out << c;
out.flush();
out.close();
std::cout << strerror(errno) << std::endl; // results with "Invalid or incomplete multibyte or wide character" on GCC

Is there any reliable way to output wchar_t into a binary stream without worrying about codecvt that can suddenly kick in and break everything? I mean I am using binary mode exactly because I don't need codecvt here.

I know that its better to use ofstream for binary output, but unfortunately usage of wofstream is part of API that I cannot change (Although I see that it is an incredibly stupid idea, at least because wchar_t size is not defined by the standard).

Update: looks like the following custom codecvt helps if I imbue it into the stream, but I am not sure if this is the right way:

class noconvCodecvt : public std::codecvt<wchar_t, char, mbstate_t>
public:
    virtual bool do_always_noconv() const throw() override
        return true;
                C++ streams from standard library are broken beyond repair. If you need a simple cross-platform equivalent, write a class that wraps a FILE* from <cstdio>, like there: github.com/Const-me/nanovg/blob/master/src/FontStash2/…
– Soonts
                Sep 6, 2020 at 22:58
                just avoid wide char and wide streams and use UTF-8 instead. You can link with the new MSVC stdlib to use UTF-8 as a locale in older Windows. See What is the Windows equivalent for en_US.UTF-8 locale?. Alternatively use Boost.Nowide
– phuclv
                Sep 7, 2020 at 1:56
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.