向文件中输入汉字

来源:互联网 发布:淘客网站采集规则php 编辑:程序博客网 时间:2024/04/30 13:47

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
char a[]="Hello I am 黄景天.你好,你是谁啊?";
ofstream of("E://text.txt");
if(of==NULL)
{
cerr<<"The file was not opened."<<endl;
exit(1);
}


int len=strlen(a);
unsigned char *p=(unsigned char *)a;
while(*p)
{
if(*p<128)
{
of.write((char *)p,sizeof(char));
p=p+1;

}
else
{
of.write((char *)p,sizeof(char));
of.write((char *)&p[1],sizeof(char));
p+=2;

}
}
cout<<"succssful."<<endl;

system("pause");
return 0;
}