Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

wofstream-question

24 views
Skip to first unread message

Bonita Montero

unread,
Feb 5, 2020, 10:35:49 AM2/5/20
to
If I create a wofsteam with MSVC, I get a simple ASCII-output, i.e. the
Unicode-codepoints aren't translated to UTF-8. I can't even output a
Unicode-character >= '\u0100', i.e. the failbit will be set then. Can
anyone here tell me how to create a output stream with UTF-8-encoding ?

Bo Persson

unread,
Feb 5, 2020, 11:01:50 AM2/5/20
to
This has not so much to do with the stream as with the locale.


Bo Persson

Ben Bacarisse

unread,
Feb 5, 2020, 11:06:12 AM2/5/20
to
Bonita Montero <Bonita....@gmail.com> writes:

> If I create a wofsteam with MSVC, I get a simple ASCII-output, i.e. the
> Unicode-codepoints aren't translated to UTF-8. I can't even output a
> Unicode-character >= '\u0100',

That's not a wide character but a multi-character constant. You want

L'\x0100'

instead. That's a value of type wchar_t.

> i.e. the failbit will be set then. Can
> anyone here tell me how to create a output stream with UTF-8-encoding ?

On my system, this works as expected:

#include <fstream>
#include <locale>

int main()
{
std::wofstream wos("out");
wos.imbue(std::locale("en_GB.UTF8"));
wos << L'\x0100';
}

but that's with g++ on Linux not MSVC.

--
Ben.

Alf P. Steinbach

unread,
Feb 5, 2020, 3:06:10 PM2/5/20
to
Well, a wide output stream translates from the wide encoding (UTF-16 in
Windows) to the external byte encoding (the ANSI codepage in Windows).

Windows only recently, in the May 2019 update of Windows 10, got support
for UTF-8 as ANSI encoding. To enable that for your application you can
add a special manifest setting with a brittle spaces-are-significant
syntax. See <url:
https://alfps.wordpress.com/2020/01/03/a-windows-h-wrapper-for-utf-8-windows-apps/>
for details.

Alternatively, if you're only using Visual C++ and don't plan on
supporting other compilers, then you can use `_setmode`. See (currently)
<url:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=vs-2019>.

Third alternative, there is a Boost sublibrary for UTF-8 i/o. When I
checked it out, right around its Boost adoption, because its author
referred to a blog article of mine, it was brittle and with some trivial
nasty bugs (like forgetting to remove carriage return in input). But
maybe it has benefited from Boost-ing, and it may do the job for you.

<rant>
This is IMO all very Windows-specific, not C++, i.e. off-topic here,
except for iostreams' curious lack of support for encodings, which is
needed critical functionality, and the ditto curious forced use of
[beep] archaic & over-engineered complex limited locale functionality,
which is not needed and cause a very undesired inefficiency.
</rant>

- Alf

Alf P. Steinbach

unread,
Feb 5, 2020, 3:07:14 PM2/5/20
to
Windows still does not have an UTF-8 locale for ordinary applications.

Since May 2019 one may however outfit an application with a manifest
that causes it to have UTF-8 as the process ANSI codepage.


- Alf


Ralf Goertz

unread,
Feb 6, 2020, 2:46:08 AM2/6/20
to
Am Wed, 05 Feb 2020 16:05:45 +0000
schrieb Ben Bacarisse <ben.u...@bsb.me.uk>:
Just adding that for this to work with std::wcout one also needs

std::ios::sync_with_stdio(false);

before the output line.


0 new messages