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:

......
524336

524766

525226

525664

526154

526572

527140

527571

-1

-1

-1

-1
.......
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"’";
_tprintf(L"%s",omega.GetBuffer());

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
std::locale::global(utf8_locale);

// Send the UCS-4 data out, converting to UTF-8
{
std::wofstream ofs("data.ucd");
ofs.imbue(utf8_locale);
std::copy(ucs4_data.begin(),ucs4_data.end(),
std::ostream_iterator<ucs4_t,ucs4_t>(ofs));
}

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 {
#define BOOST_UTF8_END_NAMESPACE }
#define BOOST_UTF8_DECL
#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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.