c++怎么只读取字符串中的汉字
2015-01-06
展开全部
这个问题太有建仔橘汪设性意义了,我决定去尝试一下
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
void main()
{
ifstream infile("file.txt");
if(!infile)
cerr<<"fail to open the file"<<endl;
int num=0,total=0;
char a;
while(infile.get(a))
{
if(a>=0)
num++;
total++;
}
cout<<total<<'\t'<<num<<endl;
infile.close();
infile.open("file.txt",ifstream::in);
char *character=new char[total-num+1];
int i=0;
while(infile.get(a))
{
if(int(a)<0)
{
character[i]=a;
infile.get(a);
character[i+1]=a;
i+=2;
}
}
character[total-num]='\0';
cout<念仔<"伍闷the character in the string is"<<character<<endl;
infile.close();
}
已经多次检验,都正确,思路是汉字第一个字节的ASCII值小于零,而且汉字占用两个字节
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
void main()
{
ifstream infile("file.txt");
if(!infile)
cerr<<"fail to open the file"<<endl;
int num=0,total=0;
char a;
while(infile.get(a))
{
if(a>=0)
num++;
total++;
}
cout<<total<<'\t'<<num<<endl;
infile.close();
infile.open("file.txt",ifstream::in);
char *character=new char[total-num+1];
int i=0;
while(infile.get(a))
{
if(int(a)<0)
{
character[i]=a;
infile.get(a);
character[i+1]=a;
i+=2;
}
}
character[total-num]='\0';
cout<念仔<"伍闷the character in the string is"<<character<<endl;
infile.close();
}
已经多次检验,都正确,思路是汉字第一个字节的ASCII值小于零,而且汉字占用两个字节
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询