As long as wifstream can read themn back, it seems that the standard allows wofstream to write 80bit values:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF8&th=ab095398862f62b2&rnum=12
So the question remains: How does one write 16-bit values to the file?
Well, as with all STL problems there is a very easy solution. It is as obvious as the nose on your face. All you need to do is single-step through the code to find that the STL geniuses determine the size of the output character based upon the character attributes of the active locale. A locale is a very simple object, like they cover in DP 101 when they teach you about punchcards and all that. Sheesh. I goes without SAYING!
Here is the simple solution:
#include <locale>
#include <fstream>
using namespace std;
typedef codecvt<wchar_t, char, mbstate_t> Mybase;
// CLASS Simple_codecvt
class Simple_codecvt : public Mybase {
public:
typedef wchar_t _E;
typedef char _To;
typedef mbstate_t _St;
explicit Simple_codecvt(size_t _R = 0)
: Mybase(_R) {}
protected:
virtual result do_in(_St& _State,
const _To *_F1, const _To *_L1, const _To *& _Mid1,_E *_F2, _E *_L2, _E *& _Mid2) const
{return (noconv); }
virtual result do_out(_St& _State, const _E *_F1, const _E *_L1, const _E *& _Mid1,_To *_F2, _To *_L2, _To *& _Mid2) const
{return (noconv); }
virtual result do_unshift(_St& _State,_To *_F2, _To *_L2, _To *& _Mid2) const
{return (noconv); }
virtual int do_length(_St& _State, const _To *_F1,const _To *_L1, size_t _N2) const _THROW0()
{return (_N2 < (size_t)(_L1 - _F1)? _N2 : _L1 - _F1); }
virtual bool do_always_noconv() const _THROW0(){return (true); }
virtual int do_max_length() const _THROW0(){return (2); }
virtual int do_encoding() const _THROW0(){return (2); }
void main()
wstring wstr = L"This is UNICODE";
locale loc = _ADDFAC(locale::classic(),
new Simple_codecvt);
wofstream wofs;
wofs.imbue(loc);
wofs.open( "c:\\temp\\myfile.bin", ios_base::out | ios_base::trunc | ios_base::binary);
wofs << wstr << endl;
=-=-=-=-=-=-=-
-- Dan
P.S. I know that most programmer types are too literal to understand sarcasm, so to the above description, please add: