From: Frank Birbacher on
Hi!

I'm facing a problem with the following code that uses a custom codecvt
facet for wide streams. The facet is taken from Boost (actually a recent
svn trunk checkout, but the considered code did not change a while, a
1.34 or 1.35 release would do) and converts wchar_t internally and UTF-8
externally.

#include <iostream>
#include <ostream>
#include <fstream>

#define BOOST_UTF8_BEGIN_NAMESPACE
#define BOOST_UTF8_DECL
#define BOOST_UTF8_END_NAMESPACE
#include <libs/detail/utf8_codecvt_facet.cpp>
#undef BOOST_UTF8_DECL
#undef BOOST_UTF8_BEGIN_NAMESPACE
#undef BOOST_UTF8_END_NAMESPACE

using namespace std;

void setUTF8Locale()
{
//try *with* and *without*
//ios_base::sync_with_stdio(false);
locale utf8Locale(locale(), new utf8_codecvt_facet);
wcout.imbue(utf8Locale);
locale::global(utf8Locale);
}

int main()
{
setUTF8Locale();

wcout << L"wert���" << endl;

wofstream out("test");
out << L"wert���\n";
}

The code is in a UTF-8 encoded source file and is compiled using gcc
4.1.2 on Gentoo Linux which accepts UTF-8 input. Well the output of the
program in an UTF-8 console is different from the content of the file
"test" written by the same program (called cvttest):

$./cvttest | hexdump -C
00000000 77 65 72 74 3f 3f 3f 0a |wert???.|
00000008
$hexdump -C test
00000000 77 65 72 74 c3 bc c3 a4 c3 b6 0a |wert.......|
0000000b
$

The wcout output converted every non-ASCII char into a question mark. A
simple cout << "I'm here" in the source code of the codecvt shows the
code is never called.

When I use sync_with_stdio(false), however, the output is the same as
the contents of the test file.

The "Standard C++ IOStreams and Locales" [Langer&Kreft] state "The first
operation that is performed on a C file determines its orientation"
(page 59) meaning: deciding between narrow and wide output. As I'm not
using the stdio here, sould sync_with_stdio possibly affect the use of a
code conversion facet?

Regards, Frank

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]