Here's good question for the gurus around here.

So we got a wofstream output.

In it I send using a recursive function time by time a CString data. Using off course the GetBuffer member to get a pointer to the content.

The problem is that it works really good till the 1207-calll.
Then suddenly the wostream becomes unusuable, invlaid and refuses to print anything else to it. The quuestion is why ?

I print with the tellp() the current size of the file and the result si something like this:












The data sent t it is around 500 char long but shouldn't be a issue. Any ideas ? How to identify the problem ? The 1207th String is the same as the prior 1206 strings. Mainly composed of chars plus these simbols : [ ] and of course some spaces.

Some update: I get a "Fatal I/O error!" and the strange thing is that the first part of the item where it gets the error gets printed.

Got the problem. It was a char not known by my current Codecvt Facet.

Mainly this : ′ . At first I saw a few of the same type earlier added so I thougt that couldn't be a problem but it seems that my Codecvt Facet is old and I need to improve it.

I'm glad you found the problem yourself because I for one have no idea what you were trying to describe. Never heard of wofstream or Codecvt Facet , but I am very familary for MFC's CString c++ class.

Of the Codecvt Facet me neither until I meet with the problem. But hey we learn every day something new.

Ok traced back to the core the problem.

CString omega = L"’";

with result:


So the problem si to make the ’ appear on the screen cause as you can see the happens within a file. Just as the cout handles the ' char (code 8217) adn puts instead of it a ? mark a ofstream (wofstream -> for wide chars -Unicode) can't do the same and puts a fail flag.

So any solution out there ?

Strangely i tried to assign a beeter codecvt Facet to my ofstream file and I get some strange errors:

Well shortly presented. We have this code form the Boost site.

// My encoding type
typedef wchar_t ucs4_t;

std::locale old_locale;
std::locale utf8_locale(old_locale,new utf8_codecvt_facet<ucs4_t>);

// Set a New global locale

// Send the UCS-4 data out, converting to UTF-8
std::wofstream ofs("data.ucd");

And the error messages:

: error C2661: 'std::locale::facet::operator new' : no overloaded function takes 3 arguments
: error C2059: syntax error : ')'
: error C2065: 'utf8_locale' : undeclared identifier
: error C2065: 'ucs4_data' : undeclared identifier
: error C2228: left of '.begin' must have class/struct/union

Any ideas why ? And mainly how to fix it ?

I'm using something wrong ? Any other advice.

>Never heard of wofstream or Codecvt Facet
wofstream is the wide character variant of ofstream. codecvt is basically meant for converting between different character encodings.

>Any ideas why ? And mainly how to fix it ?
I'd say that you either don't have Boost installed, didn't include the correct header, or both.

And aI say neither.

I've added the source and made the declarations. Now if I wouldn't have Boost this line should be marked as compiler errors.

#define BOOST_UTF8_BEGIN_NAMESPACE namespace mynamespace {
#include "boost\detail\utf8_codecvt_facet.hpp"

and the code you already know......

Can someone get to print (file or screan) this:

CString omega = L"’Ïðèâåò";


And here it is the answer to my question:

Method to write:

std::ofstream outFile("filename.dat", std::ios::out | std::ios::binary);
outfile.write((char *) wstr.c_str(), wstr.length() * sizeof(wchar_t));

Method to signal to the notepad that's Unicode:

wchar_t BOM = 0xFEFF;
std::ofstream outFile("filename.dat", std::ios::out | std::ios::binary);
outfile.write((char *) &BOM,sizeof(wchar_t));

Finally problem solved.