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

C++函数。只能传英文参数,不能传中文参数咋回事~得要死啊弄了好久~

2012-04-22 
C++函数。只能传英文参数,不能传中文参数怎么回事~急得要死啊!!~~~弄了好久~~~这样写可以正确查找到文件a.t

C++函数。只能传英文参数,不能传中文参数怎么回事~急得要死啊!!~~~弄了好久~~~
这样写可以正确查找到文件a.txt中有多少个中文关键字:

int FindKeyword(const string& keyword)
{
string line;
  ifstream in_file;
  int count = 0,pos;
in_file.open("e:\\a.txt",ios::in);
  while(getline(in_file,line)){
pos = line.find(keyword);
while(pos !=-1){
count++;//统计单词出现的个数
line = line.substr(pos+keyword.length());//查找下一个单词
pos = line.find(keyword);
}
}//当文件还保存更多行时
return count;
}
当我把在文件中查找改为在字符串string中查找时,就出现问题了!!~~若查找“朋友”返回0,若查找“ab”就会返回1~就是说只能查找英文,不能查找中文了!!怎么回事啊~~急得要死啊!!!~~代码如下:

int FindKeyword(const string& keyword)
{
string line="我们是好朋友abcd";
  int count = 0,pos;
pos = line.find(keyword);
while(pos !=-1){
count++;//统计单词出现的个数
line = line.substr(pos+keyword.length());//查找下一个单词
pos = line.find(keyword);
}
return count;
}

求各位大侠帮忙解决一下~感激涕零~
如果实在不行,那就有更好的在字符串中检索另一字符串的方法提供一下也可以~C++语言哈~万分感谢!!!!~~~


[解决办法]
转化为编码搜索
[解决办法]
如果是getline(in_file,line)语句得不到中文字体的话,可以用local函数设置下。。
[解决办法]
试试wstring
[解决办法]
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
int FindKeyword(const wstring& keyword)
{
wstring line=TEXT("我们是好朋友abcd");
int count = 0,pos;
pos = line.find(keyword);
while(pos !=-1){
count++;//统计单词出现的个数
line = line.substr(pos+keyword.length());//查找下一个单词
pos = line.find(keyword);
}
return count;
}
int main()
{

cout << FindKeyword(TEXT("朋友")) << endl;
}
完全没问题,OK
[解决办法]
定义两个版本的函数,一个版本为UNICODE,一个版本为非UNICODE的然后根据是否定义了_UNICODE来调用。

热点排行