Solve the problem that cfile outputs Chinese characters as garbled characters

Source: Internet
Author: User

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?
  1. Cfile fileout;
  2. Fileout. Open (_ T ("report.txt"), cfile: writable exclusive | cfile: modewrite | cfile: modecreate );
  3. Cstring strtitle = _ T ("Chinese character/R/N ");
  4. 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?
  1. # Difine tcharlen (sizeof (tchar) // The number of bytes occupied by a single character
  2. Cfile fileout;
  3. Fileout. Open (_ T ("report.txt"), cfile: writable exclusive | cfile: modewrite | cfile: modecreate );
  4. Ulonglong dwfilelen = fileout. getlength ();
  5. If(0 = dwfilelen)
    // When the file is empty, the Unicode bytecode mark is written.
  6. {
  7. ConstUnsigned
    CharLeadbytes [] = {0xff, 0xfe };
  8. Fileout. Write (leadbytes,Sizeof(Leadbytes ));
  9. }
  10. Cstring strtitle;
  11. Strtitle = _ T ("Chinese characters/R/N ");
  12. 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 .......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.