[C/C++标准库]_[初级]_[读写中文路径的文件--写入unicode字符串]


场景:

1. 需要写入非ascii文本并且与本地编码无关时,除了utf8,unicode编码是另外一个选择,它的好处是占两个字节,便于统计字符和对字符进行处理,因为有对应的宽字节的函数,如wcslen.

2.使用_wfopen支持中文路径.


代码1,写入二进制值:

#include 
#include 
#include 
#include 

int main(int argc, char const *argv[])
{
    const wchar_t* str = L"a中国人的国家asdfamn12312";
    FILE* file = _wfopen(L"E:\\中文.txt",L"w");
    char header[]={0xff,0xfe};
    fwrite(header,1,sizeof(header),file);
    printf("%d\n",wcslen(str)*2);
    fwrite(str,1,wcslen(str)*2,file);
    //fwprintf(file,L"%S",str);以ANSI模式打开文件,这个函数时用不了的.
    fclose(file);
    return 0;
}


fwprintf is a wide-character version offprintf; infwprintf,format is a wide-character string. These functi

你可能感兴趣的:(C/C++标准库,系统平台)