HighTechTalks DotNet Forums  

Unicode and ofstream

Dotnet Internationalization microsoft.public.dotnet.internationalization


Discuss Unicode and ofstream in the Dotnet Internationalization forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
TomTom
 
Posts: n/a

Default Unicode and ofstream - 02-19-2005 , 07:23 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



Reply With Quote
  #2  
Old   
Ted
 
Posts: n/a

Default Re: Unicode and ofstream - 02-20-2005 , 12:02 AM






use wofstream and read
http://www.codeproject.com/vcpp/stl/...stounicode.asp

and

http://groups.google.co.uk/groups?hl...rldnet.att.net

Ted.

"TomTom" <no_spam (AT) nospamfordiscussion (DOT) com> wrote

Quote:
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





Reply With Quote
  #3  
Old   
Ted
 
Posts: n/a

Default Re: Unicode and ofstream - 02-20-2005 , 12:15 AM



P.S. and don't forget the BOM (byte order mark) - mentioned in one of the
comments in the codeproject article.

Ted.

"Ted" <ted (AT) t--x (DOT) org> wrote

Quote:
use wofstream and read
http://www.codeproject.com/vcpp/stl/...stounicode.asp

and


http://groups.google.co.uk/groups?hl...rldnet.att.net

Ted.

"TomTom" <no_spam (AT) nospamfordiscussion (DOT) com> wrote in message
news:eL50jJuFFHA.2180 (AT) TK2MSFTNGP10 (DOT) phx.gbl...
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







Reply With Quote
  #4  
Old   
TomTom
 
Posts: n/a

Default Re: Unicode and ofstream - 02-20-2005 , 04:10 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();

}




"Ted" <ted (AT) t--x (DOT) org> wrote

Quote:
P.S. and don't forget the BOM (byte order mark) - mentioned in one of the
comments in the codeproject article.

Ted.

"Ted" <ted (AT) t--x (DOT) org> wrote in message
news:uHrySlwFFHA.2296 (AT) TK2MSFTNGP15 (DOT) phx.gbl...
use wofstream and read
http://www.codeproject.com/vcpp/stl/...stounicode.asp

and


http://groups.google.co.uk/groups?hl...rldnet.att.net

Ted.

"TomTom" <no_spam (AT) nospamfordiscussion (DOT) com> wrote in message
news:eL50jJuFFHA.2180 (AT) TK2MSFTNGP10 (DOT) phx.gbl...
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









Reply With Quote
  #5  
Old   
Ted
 
Posts: n/a

Default Re: Unicode and ofstream - 02-20-2005 , 10:00 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:ut|ios::binary);
fout << wchar_t(0xFEFF);

Ted.

"TomTom" <no_spam (AT) nospamfordiscussion (DOT) com> wrote

Quote:
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();

}




"Ted" <ted (AT) t--x (DOT) org> wrote in message
news:%23B1NpswFFHA.1476 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
P.S. and don't forget the BOM (byte order mark) - mentioned in one of
the
comments in the codeproject article.

Ted.

"Ted" <ted (AT) t--x (DOT) org> wrote in message
news:uHrySlwFFHA.2296 (AT) TK2MSFTNGP15 (DOT) phx.gbl...
use wofstream and read
http://www.codeproject.com/vcpp/stl/...stounicode.asp

and



http://groups.google.co.uk/groups?hl...rldnet.att.net

Ted.

"TomTom" <no_spam (AT) nospamfordiscussion (DOT) com> wrote in message
news:eL50jJuFFHA.2180 (AT) TK2MSFTNGP10 (DOT) phx.gbl...
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











Reply With Quote
  #6  
Old   
TomTom
 
Posts: n/a

Default Re: Unicode and ofstream - 02-20-2005 , 05:59 PM



Great.. Now there is no text corruption.

Thank you!


"Ted" <ted (AT) t--x (DOT) org> wrote

Quote:
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:ut|ios::binary);
fout << wchar_t(0xFEFF);

Ted.

"TomTom" <no_spam (AT) nospamfordiscussion (DOT) com> wrote in message
news:O0M4awyFFHA.2156 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
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();

}




"Ted" <ted (AT) t--x (DOT) org> wrote in message
news:%23B1NpswFFHA.1476 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
P.S. and don't forget the BOM (byte order mark) - mentioned in one of
the
comments in the codeproject article.

Ted.

"Ted" <ted (AT) t--x (DOT) org> wrote in message
news:uHrySlwFFHA.2296 (AT) TK2MSFTNGP15 (DOT) phx.gbl...
use wofstream and read
http://www.codeproject.com/vcpp/stl/...stounicode.asp

and



http://groups.google.co.uk/groups?hl...rldnet.att.net

Ted.

"TomTom" <no_spam (AT) nospamfordiscussion (DOT) com> wrote in message
news:eL50jJuFFHA.2180 (AT) TK2MSFTNGP10 (DOT) phx.gbl...
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













Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.