CFile write unicode text in wince
Hello everybody
I'm trying to write some texts into html file using CFile::Write function. Below is what I'm using.
mFile.Write(myString,myString.GetLength() * sizeof(TCHAR));
As a result in the file however I get something like this:
0 1 : 2 2 : 0 6 . 0 0 T a e o t u i e o e
This should be written as:
01:22:06.00 Trace output file open
Any idea what the problem could be?
Thanks in advance!
Re: CFile write unicode text in wince
Hi,
You are writing the file as unicode, but when you view it are opening it as ascii (one byte per character.)
Since it's HTML, you probably want to write it as UTF8, and can use WideCharToMultiByte to convert to that. Use google to find a suitable sample.
Alan
Re: CFile write unicode text in wince
Just to make sure, you meant MultiByteToWideChar conversion, correct?
Re: CFile write unicode text in wince
Hi,
In fact, I did mean WideCharToMultiByte. It sounds wrong, but WideChar is 16bit, MultiByte is 8bit.
Alan
Re: CFile write unicode text in wince
Thanks for the replies Alan
So I modified the codes as follows, but I'm still getting the same result
char StringToWrite[500];
WideCharToMultiByte(CP_ACP,NULL,myString,-1,StringToWrite,sizeof(StringToWrite),NULL,NULL);
mFile.Write(StringToWrite,StringToWrite.GetLength() * sizeof(TCHAR));
Any ideas?
Re: CFile write unicode text in wince
Hi,
This looks wrong: StringToWrite.GetLength(), did you show your exact code after changing it?
Alan
Re: CFile write unicode text in wince
Yes the write function was called through another function which gets (CString myString) as argument. When I passed char[] type, it worked without complaining.
I managed to write the text part now correctly.
Now, I still have the timestamp part wrong. What could be the problem?
I'm doing the following for the time:
CString m_Hour,m_Min,m_Sec;
CTime currentTime = CTime::GetCurrentTime();
m_Hour.Format(_T("%02d"), currentTime.GetHour());
m_Min.Format(_T("%02d"), currentTime.GetMinute());
m_Sec.Format(_T("%02d"), currentTime.GetSecond());
m_Ms = "00";
// Formatting stuff
CString timeString ;
// hour
if (m_ShowHour)
{
timeString += m_Hour;
}
// min
if (m_ShowMin)
{
if (m_ShowHour)
{
timeString += ":";
}
timeString += m_Min;
}
// sec
if (m_ShowSec)
{
if (m_ShowMin || m_ShowHour)
{
timeString += ":";
}
timeString += m_Sec;
}
// ms
if (m_ShowMs)
{
if (m_ShowSec || m_ShowMin || m_ShowHour)
{
timeString += ".";
}
timeString += m_Ms;
}
// Now the write to file part
mFile.Write(timeString ,timeString.GetLength() * sizeof(TCHAR));
Re: CFile write unicode text in wince
Well, it's still the same problem, you are writing it out with 2bytes per character, you need to convert to UTF8.