[C + + standard library]_[read and write Chinese path files--write Unicode strings]

Source: Internet
Author: User


Scene:

1. If you need to write non-ASCII text and is not relevant to the local encoding, in addition to the Utf8,unicode encoding is another option, it has the advantage of two bytes to facilitate statistical characters and character processing, because there are corresponding wide-byte functions, such as wcslen.

2. Highlights that need attention, write the 0xff,0xfe file header first, and then write a wide-byte string in%s (uppercase) format when using fwprintf.

3. Use _wfopen to support Chinese path.


Code Listing 1:

#include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <string.h>int main (int argc    , Char const *argv[]) {    const wchar_t* str = L "A Chinese national asdfamn12312";    file* file = _wfopen (l "e:\\ Chinese. txt", l "w");    Char Header[]={0xff,0xfe};    Fwrite (header,1,sizeof (header), file);    printf ("%d\n", wcslen (str) * *);    Fwrite (Str,1,wcslen (str) *2,file);    fwprintf (file,l "%s", str); Open the file in ANSI mode, this function is not available.    fclose (file); return 0;}


fwprintf is a wide-character version offprintf; In fwprintf,format is a wide-character string. These functions behave identically if the stream is opened in ANSI mode. fprintf does not currently support output to a UNICODE stream.

Http://msdn.microsoft.com/en-us/library/xkh07fe2.aspx


If you want to use fwprintf, open the file in Unicode mode.

Http://msdn.microsoft.com/en-us/library/yeby3zcb.aspx


Code Listing 2:

#include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <string.h> #define _ Unicodeint Main (int argc, char const *argv[]) {const wchar_t* str = L "A Chinese country asdfamn12312"; file* file = _wfopen (l "e:\\ Chinese. txt", l "W, Ccs=unicode"), Char header[]={0xff,0xfe};//fwrite (header,1,sizeof (header), File);p rintf ("%d\n", Wcslen (str)),//fwrite (Str,1,wcslen (str) *2,file); fwprintf (file,l "%s", str); fclose (file); return 0;}





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.