Groups | Blog | Home
all groups > dotnet internationalization > february 2005 >

dotnet internationalization : Unicode and ofstream


TomTom
2/19/2005 4:23:15 PM
Hi,

I have a very simple program below. I am trying to output Unicode characters
in a file.

-----------

ofstream logFile("myLog.txt");
logFile << L"ABC"; // In the actual scenario, the characters will
include multinational characters.

---------

After the program is run, the myLog.txt includes:

004980DC

Is there a Unicode counterpart of ofstream? Or do I need to somehow convert
the numbers above to actual characters?

By the way, MFC cannot be used in my environment.

Thanks much,
TomTom

Ted
2/20/2005 12:02:10 AM
use wofstream and read
http://www.codeproject.com/vcpp/stl/upgradingstlappstounicode.asp

and

http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=7cqqst%24p64%40bgtnsc03.worldnet.att.net

Ted.

[quoted text, click to view]

Ted
2/20/2005 12:15:19 AM
P.S. and don't forget the BOM (byte order mark) - mentioned in one of the
comments in the codeproject article.

Ted.

[quoted text, click to view]

TomTom
2/20/2005 1:10:55 AM
Thanks, Ted. I could make it work for Western European characters such as
"ABC", but I see nothing for the non-European chars, such as Japanese. I
saved the NotePad as Unicode, but then I see garbage instead of the Japanese
chars.

The code looks like this.

void WriteToLog(BSTR myText)
{
wstring convertedText = wstring(uiText, lstrlen(myText));
char logFileName[] = "myLog.txt";
wofstream logFile(logFileName, ios::app);
logFile << convertedText.c_str() << L"\n";
logFile.close();

}




[quoted text, click to view]

Ted
2/20/2005 10:00:02 AM
To get it to work with those characters, you must use binary and follow the
advice in the links that I gave you (e.g. write BOM)

wofstream fout;
fout.imbue( locale(locale::classic(), new NullCodecvt) );
fout.open(sPath.c_str(), ios::out|ios::binary);
fout << wchar_t(0xFEFF);

Ted.

[quoted text, click to view]

TomTom
2/20/2005 2:59:35 PM
Great.. Now there is no text corruption.

Thank you!


[quoted text, click to view]

AddThis Social Bookmark Button