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;}