File IO meets Chinese Characters

Source: Internet
Author: User

I originally wanted to use a file to save several strings, including Chinese character strings. Because it is made of MFC, I naturally want to use the CFile class provided by MFC to file I/O. However, CFile cannot read a record (a string separated by carriage returns. So I switched to the CStdioFile class, but the CStdioFile: WriteString and Readstring do not support Chinese Characters During file IO, or they cannot be written into the file, you cannot read Chinese characters from files. I have tried both the text and binary modes. Text files created by yourself with UNICODE encoding cannot be read normally. It was not done in the middle of the night. I am angry. Therefore, I/O streams of standard files in C ++ are used to access ANSI files. Then convert the ANSI string to the Unicode string. The problem is finally solved. Converts ANSI to Unicode (1) using the L macro. For example, CLSIDFromProgID (L "MAPI. folder ", & clsid); (2) Implement conversion through the MultiByteToWideChar function, for example, char * szProgID =" MAPI. folder "; WCHAR szWideProgID [128]; CLSID clsid; long lLen = substring (CP_ACP, 0, szProgID, strlen (szProgID), szWideProgID, sizeof (szWideProgID )); szWideProgID [lLen] = '\ 0'; (3) implemented through the A2W macro, for example, USES_CONVERSION; CLSIDFromProgID (A2W (szProgID), & clsid ); convert Unicode to ANSI (1) Using WideCharToMultiByte. For example: // assume that a Unicode string wszSomeString is already in use... char szANSIString [MAX_PATH]; WideCharToMultiByte (CP_ACP, signature, wszSomeString,-1, szANSIString, sizeof (szANSIString), NULL, NULL); (2) use the W2A macro, for example: USES_CONVERSION; pTemp = W2A (wszSomeString );

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.