在.txt文件中比较字符串字与行 - 语言限制?

时间:2017-01-15 16:02:14

标签: c++ string dictionary setlocale regional-settings

我正在为我的考试项目制作一些简单的拼写检查/词典。 没有什么特别的,但我在比较使用区域字符的单词时遇到了问题 - 在这种情况下是波兰语。

它是如何工作的?  1.用户输入特定字词。  2.如果用户的单词存在于字典(a.txt)中,则进行程序检查。  3.程序显示结果。

出于测试目的,我将字典限制为'a'字样,因为整个字典中的波兰重约38MB,所以我必须实现某种alghoritm以加快搜索过程,但事实并非如此现在。

基本上程序运行正常。当用户输入诸如:auto,abakus,atom之类的单词时 - 一切都很好。 但是当用户输入包含区域符号的单词时会出现错误,例如'abażur','aranżując'等。

setLocale并没有真正帮助。 我试图将每个区域字符归因于一个特定的ascii数字 - 它实际上适用于cout和cin,但不适用于比较字符串。 还有什么建议吗?我的想法已经不多了。

int main() {
setlocale(LC_ALL, "");

string line;
string word;
cout << tr("Zazółć gęślą jaźń") << endl;    //test line1
cout << "Abażur" << endl;                   //test line2
cout << "Enter word: ";
cin >> word;
ifstream myfile("a.txt");                   //open dictionary
if (myfile.is_open())
{
    while (getline(myfile, line))
    {
        //cout << line << '\n';
        if (word == line)                   //compare current words
        {
            cout << "Word exist in dictionary!" << endl << word << endl << line << endl;
            flag1 = 1;
            myfile.close();
        }
    }
    if (flag1 == 1)
    {
        cout << "Search successful!" << endl;
        myfile.close();
    }
    else
        cout << "Search failed! No such word in dictionary." << endl;
        myfile.close();
}
else cout << "Unable to open file";
cin >> a;
return 0;
}

0 个答案:

没有答案
相关问题