[C/C++标准库]_[初级]_[读写中文路径的文件--写入unicode字符串]
来源:互联网 发布:iphone手机阅读软件 编辑:程序博客网 时间:2024/06/05 05:34
场景:
1. 需要写入非ascii文本并且与本地编码无关时,除了utf8,unicode编码是另外一个选择,它的好处是占两个字节,便于统计字符和对字符进行处理,因为有对应的宽字节的函数,如wcslen.
代码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中国人的国家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 functions behave identically if the stream is opened in ANSI mode.fprintf does not currently support output into a UNICODE stream.
http://msdn.microsoft.com/en-us/library/xkh07fe2.aspx
如果要使用fwprintf的话,要以unicode模式打开文件.
http://msdn.microsoft.com/en-us/library/yeby3zcb.aspx
代码2,写入unicode字符串:
#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中国人的国家asdfamn12312";FILE* file = _wfopen(L"E:\\中文.txt",L"w, ccs=UNICODE");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);fclose(file);return 0;}
0 0
- [C/C++标准库]_[初级]_[读写中文路径的文件--写入unicode字符串]
- [C/C++标准库]_[初级]_[分割字符串Split]
- [C/C++标准库]_[初级]_[构造文件路径(stringByAppendingPathComponent)]
- [C/C++标准库]_[初级]_[std::vector的多线程读写问题]
- [C/C++标准库]_[初级]_[C++ iostream read getline 读取文件慢的原因]
- [C/C++标准库]_[初级]_[获取文件的所在目录跨平台实现]
- [C/C++]_[初级]_[标准字符串std::string 所有函数的用法总结]
- [C/C++标准库]_[初级]_[不使用正则替换和删除字符串]
- [C/C++标准库]_[初级]_[字符串string转换大小写]
- [C/C++标准库]_[初级]_[使用模板删除字符串前后空格((w)string space)]
- [C/C++标准库]_[初级]_[使用时间库]
- [C/C++标准库]_[初级]_[signal信号浅析]
- [C/C++标准库]_[初级]_[集合操作]
- [C/C++标准库]_[初级]_[使用string stream]
- [C/C++标准库]_[初级]_[unary_function 和 binary_function]
- [C/C++标准库]_[初级]_[unary_function 和 binary_function]
- [C/C++标准库]_[初级]_[标准库里提供的排序算法]
- C++_文件读写
- 基于大数据分析的网络攻击检测
- cxf快速发布webService之aegis
- va_start(), va_arg(), va_end() 宏定义的解释
- Jquery的模拟事件,input模拟focus
- 正则表达式笔记 4 分支条件 逻辑操作符 |
- [C/C++标准库]_[初级]_[读写中文路径的文件--写入unicode字符串]
- Partition List
- Btree在kafka消息持久化方面的应用
- liferay-tomcat注册服务的方法和之后产生的乱码问题的解决办法
- JNI中cpp文件编写,解决Unresolved inclusion问题
- SpringMvc 配置 hibernate
- (数学,大数运算)Consecutively Increasing Sequences_ACdream原创群赛(17)のacmer never retire
- 展讯8825分区表
- Cocos2d-x利用jni调用java层代码