When running a program, you usually need to record some log files. The common method is to output the program running information to the TXT text.
The Unicode is used in windows and ANSI.
In wince, Unicode dual-byte encoding is used by default, instead of the ANSI single-byte of the system,
Therefore, when writing TXT text, you need to make some settings before you can view it without garbled characters.
To put it bluntly, we need to write Unicode-encoded TXT text.
The difference between unicode encoded text and ANSI encoded text is that the first two bytes of Unicode text are FF Fe.
Therefore, when creating a text in wince, you must first write two bytes of content FF Fe, indicating that the text is Unicode.
When calling writefile, you also need to note the length of the string to be written.
Since Unicode is dubyte and writefile is calculated in bytes, the Written string is multiplied by 2.
Because windows is low-byte front and high-byte back, if you set the Unicode flag
Word wunicodeflag = 0 xfeff;
Boolwritetofile (lpctstr lpdata) <br/>{< br/> bool bresult = false; <br/> handle hfile; <br/> DWORD dwbytewrite; <br/> tchar szbuf [256]; </P> <p> hfile = createfile (text ("// regrecord.txt"), <br/> generic_read | generic_write, <br/> 0, // share mode <br/> null, // lpsecurityattribute <br/> open_always, // create disposition <br/> file_attribute_normal, <br/> null); <br/> If (hfile = invalid_handle_value) <br/> {<br/> Stringcchprintf (szbuf, 256, text ("writefile: % d"), getlasterror (); <br/> MessageBox (null, szbuf, text ("error "), mb_iconerror); <br/> return bresult; <br/>}< br/> // write the Unicode flag <br/> If (getlasterror ()! = Error_already_exists) <br/>{< br/> word wunicodeflag; <br/> wunicodeflag = 0 xfeff; <br/> If (! Writefile (hfile, & wunicodeflag, sizeof (Word), & dwbytewrite, null) <br/>{< br/> stringcchprintf (szbuf, 256, text ("writefile: % d "), getlasterror (); <br/> MessageBox (null, szbuf, text (" error "), mb_iconerror ); <br/>}< br/> setfilepointer (hfile, 0, null, file_end); </P> <p> If (! Writefile (hfile, <br/> lpdata, <br/> _ tcslen (lpdata) * sizeof (tchar), // bytes wait to write. <br/> & dwbytewrite, <br/> null) <br/> {<br/> stringcchprintf (szbuf, 256, text ("writefile: % d "), getlasterror (); <br/> MessageBox (null, szbuf, text ("error"), mb_iconerror ); <br/>}< br/> else <br/>{< br/> bresult = true; <br/>}</P> <p> closehandle (hfile ); <br/> return bresult; <br/>}