本文介绍了读取C ++中的txt文件(中文)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试开发一种功能,以检查用户输入的中文单词是否在txt文件中.以下是代码.但这是行不通的.我想知道问题是什么.请帮帮我.
I'm trying to develop function that check whether chinese word which user enters is in the txt file or not. The following is the code. But it is not working. I want to know what the problem is. Help me please.
setlocale(LC_ALL, "Chinese-simplified");
locale::global(locale("Chinese_China"));
SetConsoleOutputCP(936);
SetConsoleCP(936);
bool exist = FALSE;
cout << "\n\n <Find the keyword whether it is in that image or not> \n ";
cout << "Enter word to search for: ";
wstring search;
wcin >> search; //There is a problem to enter chinese.
wfstream file_text("./a.txt");
wstring line;
wstring::size_type pos;
while (getline(file_text, line))
{
pos = line.find(search);
if (pos != wstring::npos) // string::npos is returned if string is not found
{
cout << "Found!" << endl;
exist = true;
break;
}
}
当我使用此代码时,结果如下.
when I use this code, The result is as follows.
const int oldMbcp = _getmbcp();
_setmbcp(936);
const std::locale locale("Chinese_China.936");
_setmbcp(oldMbcp);
推荐答案
尝试locale::global(locale("Chinese_China.936"));
或locale::global(locale(""));
对于LC_ALL "chinese-simplified"
或"chs"
Try locale::global(locale("Chinese_China.936"));
or locale::global(locale(""));
And for LC_ALL "chinese-simplified"
or "chs"
这篇关于读取C ++中的txt文件(中文)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!