怎样用unicode读写显示文本文件内容
//下面的代码应该不涉及unicode编码
#include <stdafx.h>
#include <iostream >
#include <iomanip >
#include <fstream>
using namespace std;
int main()
{
fstream fin;
fin.open(L ".\\我.txt ",ios::in|ios::out|ios::app);
if(!fin)
{
cerr < < "数据文件无法打开!\n ";exit(1);
} else
{
cout < < "打开成功 " < <endl;
}
char *pstr=new char[10];
fin.read(pstr,9);
pstr[9]= '\0 ';
cout < <pstr < <endl;
for(int i=0;i <10;i++)
{
if(pstr[i]> 127)
{
cout < <pstr[i] < <pstr[i+1];//输出中文字
i++;
}
else
{
cout < <pstr[i];
}
}
char strp[]={ "woshi中国人123 "};
fin.seekg(0);
fin < <strp;//在文件末尾写入strp
delete []pstr;
return 0;
}
// ".\\我.txt "中的内容是:hi,我是3x3,我是中国人,wo ai ni i love u
[解决办法]
unicode打开并访问的方式
1. 必须以binary方式打开(Windows如此,unix不知道)
2. 文件开头必须以0xFF 0xFE开始
3. 文字转换成wchar后按照字节数组方式写入
因此很可能你不能用ofstream用operator < <来操作
[解决办法]
typedef basic_fstream <char, char_traits <char> > fstream;
不支持Unicode。
[解决办法]
用wfstream和wchar_t