It is very convenient to use cfile to operate files, but it will become garbled after outputting Chinese characters to files.
 
Most of the results obtained by searching for a solution on the Internet are as follows:
 
1. Select multi-byte mode to compile the program 2. Use the widechartomultibyte function to convert the wide character into a narrow character and then output
 
For example, the following codeMulti-byte compilation ModeOutput Chinese characters to files
 
 
[CPP] 
View plaincopyprint? 
 
 - Cfile fileout;
- Fileout. Open (_ T ("report.txt"), cfile: writable exclusive | cfile: modewrite | cfile: modecreate );
 
- Cstring strtitle = _ T ("Chinese character/R/N ");
 
- Fileout. Write (strtitle. getbuffer (), strtitle. getlength ());
Cfile fileout; <br/> fileout. open (_ T ("report.txt"), cfile: Export exclusive | cfile: modewrite | cfile: modecreate ); </P> <p> cstring strtitle = _ T ("Chinese character/R/N"); </P> <p> fileout. write (strtitle. getbuffer (), strtitle. getlength (); </P> <p>
 
 
However, in many cases, we all want to use Unicode mode for compilation. At the same time, we may wonder if we can't normally output Unicode characters to files?
 
The answers that are popular on the Internet clearly do not hit the key! In fact, Unicode characters have been faithfully entered into the file by cfile! Unfortunately, the word processing software (such as NotePad) does not know the Unicode characters, and treats them as single-byte characters.
 
To enable the word processing software to recognize Unicode, it must be on the file header.Unicode-encoded leading characters: 0xff, 0xfe.
 
Therefore, you can use the following code to output Chinese characters and open them with the word processing software without garbled characters.
 
 
[C-sharp] 
View plaincopyprint? 
 
 - # Difine tcharlen (sizeof (tchar) // The number of bytes occupied by a single character
 
- Cfile fileout;
- Fileout. Open (_ T ("report.txt"), cfile: writable exclusive | cfile: modewrite | cfile: modecreate );
 
- Ulonglong dwfilelen = fileout. getlength ();
- If(0 = dwfilelen)
 // When the file is empty, the Unicode bytecode mark is written.
 
- {
- ConstUnsigned
 CharLeadbytes [] = {0xff, 0xfe };
 
- Fileout. Write (leadbytes,Sizeof(Leadbytes ));
 
- }
- Cstring strtitle;
- Strtitle = _ T ("Chinese characters/R/N ");
 
- Fileout. Write (strtitle. getbuffer (), strtitle. getlength () * tcharlen); // Note: The number of output bytes is inconsistent with the number of characters
 
# Difine tcharlen (sizeof (tchar) // The number of bytes a character occupies </P> <p> cfile fileout; <br/> fileout. open (_ T ("report.txt"), cfile: Export exclusive | cfile: modewrite | cfile: modecreate); </P> <p> ulonglong dwfilelen = fileout. getlength (); <br/> If (0 = dwfilelen) // when the file is empty, the Unicode bytecode mark is written <br/>{< br/> const unsigned char leadbytes [] ={ 0xff, 0xfe}; <br/> fileout. write (leadbytes, sizeof (leadbytes); <br/>}</P> <p> cstring strtitle; <br/> strtitle = _ T ("Chinese character/R/N"); </P> <p> fileout. write (strtitle. getbuffer (), strtitle. getlength () * tcharlen); // Note: The number of output bytes is inconsistent with the number of characters
 
 
After finishing, close the ticket .......