首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎么让string类型的操作支持中文呢

2012-02-27 
如何让string类型的操作支持中文呢?我们的作业是写一个程序用于统计文件的一些单词的出现次数和所在行数,

如何让string类型的操作支持中文呢?
我们的作业是写一个程序用于统计文件的一些单词的出现次数和所在行数,我写的代码如下,已经满足了老师的要求,但是我自己实验的时候却不能对中文的文本进行正确的操作,可能是不支持UNICODE吧,那么要怎么改才可以支持中文呢?   谢谢!~

#include <iostream>
#include <string>
#include <fstream>

using   namespace   std;

ofstream     fp;
ifstream     ifp;
bool   check_create   =   false;
int   cline;

void   CreatTextFile()
{
if(check_create   ==   true)
{
cout < < "指定文件已经打开,该操作失败! " < <endl;
return;
}
else
{
char   fname[10];
cout < < "输入要建立的文件名以及保存路径: " < <endl;
cin> > fname;
cout < < "您要输入多少行内容? " < <endl;
cin> > cline;
cin.ignore();
fp.open(fname,   ios::ate);
cout < < "请输入内容:(共 " < <cline < < "行): " < <endl;
for(int   i   =   0;   i   <   cline;   i++)
{
char     temp[100];
gets(temp);
fp   < <   temp;
fp   < <   endl;
}
ifp.open(fname);
cout < < "该文件已经建立并被打开! " < <endl;
check_create   =   true;
}
}

void   OpenTextFile()
{
if(check_create   ==   true)
{
cout < < "指定文件已经打开,该操作失败! " < <endl;
return;
}
else
{
char   ofname[100];
                string   buffer;
cout < < "请输入该文件的路径: " < <endl;
cin> > ofname;
cin.ignore();
ifp.open(ofname);
if(ifp.fail())
cout < < "您所输入的文件不存在! " < <endl;
else
{
cout < < "文件打开成功 " < <endl;
check_create   =   true;
while(!ifp.eof())
{
getline(ifp,   buffer,   '\n ');
cline   ++;
buffer.erase();
}
cout < < "该文件有 " < <cline < < "行 " < <endl;
ifp.seekg(0);
ifp.clear();
}
}

}

void   SubStrCount()
{
if(check_create   ==   false)
{
cout < < "不存在文件被打开,该操作失败! " < <endl;
return;
}
else
{
int   count   =   0;
size_t   size   =   0;
string     word;
cout < < "请输入您想查询的单词: " < <endl;
cin> > word;
cin.ignore();

string     *content   =   new   string   [cline];
for(int   i   =   0;   i   <   cline;   i++)
{
getline(ifp,   content[i],   '\n ');
cout < < "content[i]= " < <content[i] < <endl;
while(   (size   =   content[i].find(word,size))!=string::npos   )
{
size   +=   word.size();
count   ++;
}
size   =   0;
}

if(count)
cout < < "该词出现了 " < <count < < "次 " < <endl;
else
cout < < "没有要找的词 " < <endl;
ifp.seekg(0);
ifp.clear();
}
}

void   SubStrInd()
{
if(check_create   ==   false)
{
cout < < "不存在文件被打开,该操作失败! " < <endl;
return;
}
else


{
int   count   =   0;
size_t   size   =   0;
string     *content   =   new   string   [cline];
string   word;

cout < < "输入您想定位的单词: " < <endl;
cin> > word;
cin.ignore();

for(int   i   =   0;   i   <   cline;   i++)
{
getline(ifp,   content[i],   '\n ');
while(   (size   =   content[i].find(word,size))!=string::npos   )
{
size   +=   word.size();
count   ++;
}
if(count)
cout < < "在第 " < <i+1 < < "行 " < <endl;
else
{}
count   =   0;
size   =   0;
}
ifp.seekg(0);
ifp.clear();
}
}

int   main(int   argc,   char   *argv[])
{
int   xz;

do{
cout < <endl;
cout < < "************************* " < <endl;
cout < < "*文本文件的检索   子串的统计及定位* " < <endl;
cout < < "************************* " < <endl;
cout < < "*   1   建立新文件并打开   * " < <endl;
cout < < "*   2   打开已有的文件       * " < <endl;
cout < < "*   3   单词子串的计数       * " < <endl;
cout < < "*   4   单词子串的定位       * " < <endl;
cout < < "*   5   退出整个系统           * " < <endl;
cin> > xz;
switch(xz)
{
case   1:CreatTextFile();break;
case   2:OpenTextFile();break;
case   3:SubStrCount();break;
case   4:SubStrInd();break;
case   5:exit(0);
default:cout < < "选择错误!重新选择 " < <endl;
}
}while(1);
fp.close();
ifp.close();

system( "pause ");
return   0;
}



[解决办法]
不需要考虑 Unicode 问题,
中文也可以是 ANSI 编码的,
用string 即可 ~
[解决办法]
这回支持一下虫子

[解决办法]
string支持中文啊
[解决办法]
string可以的啊,
一个汉字两个字节,

string str( "汉字 ");
cout < <str < <endl;

输出:汉字

热点排行