0

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;
    }
};
Mahbub Ul Islam
  • 773
  • 8
  • 15
Ibraim Ganiev
  • 8,934
  • 3
  • 33
  • 52
  • 2
    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 ``, like there: https://github.com/Const-me/nanovg/blob/master/src/FontStash2/FileHandles.h – Soonts Sep 06 '20 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?](https://stackoverflow.com/a/63454192/995714). Alternatively use Boost.Nowide – phuclv Sep 07 '20 at 01:56

0 Answers0