| | |
Strange wofstream problem /bug
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
Change your thought and the world around you changes.
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
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.
Change your thought and the world around you changes.
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
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.
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.
Last edited by jokerjokerer; Feb 2nd, 2008 at 6:43 pm.
Change your thought and the world around you changes.
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Of the Codecvt Facet me neither until I meet with the problem. But hey we learn every day something new.
Change your thought and the world around you changes.
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
Ok traced back to the core the problem.
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.
C++ Syntax (Toggle Plain Text)
CString omega = L"’"; _tprintf(L"%s",omega.GetBuffer());
with result:
•
•
•
•
?
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.
Last edited by jokerjokerer; Feb 3rd, 2008 at 7:16 am.
Change your thought and the world around you changes.
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
>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.
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.
New members chased away this month: 4
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......
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......
Change your thought and the world around you changes.
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
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.
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.
Change your thought and the world around you changes.
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
"In a moment, everything can change. Feel the wind on your shoulders.
For a minute, all the world can wait. Let go of your yesterday. " H.Duff-Fly
Anything is possible
![]() |
Other Threads in the C++ Forum
- Previous Thread: Multi threading problem.
- Next Thread: Help me with factorial in c++
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






