在文本文件中查找特定的单词

Finding a particular word in a text file?

本文关键字:单词 查找 文本 文件      更新时间:2023-10-16

所以我对c++和编码比较陌生,我最近尝试制作这个调查程序(请忽略可怕的代码)。我被困的地方是,在最后,当用户要求现有的信息,我没有办法找到特定的信息与名称在文本文件中。我该怎么办呢?此外,ExisitingUser之前的goto标签显示了类似于-fpermissive error的内容。我不知道那是什么

抱歉,如果这样的事情已经回答了之前。

代码:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
    char choiceInfo;
    cout << "Do you want to enter a new user or check existing info? N/En";
    cin >> choiceInfo;
    if(choiceInfo == 'N' || choiceInfo == 'n') {
    } else {
        goto existUser;
    }
    x:    
    string firstName,surName,fullName,DoB,quesOne,quesTwo,quesThree,quesFour,quesFive;
    int age;
    cout << "Enter your first name.n";
    cin >> firstName;    
    cout <<"Enter your surname.n";
    cin >> surName;
    fullName = firstName + surName;
    cout << "How old are you?n";
    cin >> age;
    cout << "What is your DoB?n Format:DD/MM/YYYYn";
    cin >> DoB;
    cout <<"What is your favorite sport?n";
    cin >> quesOne;
    cout <<"What is your favorite colour?n";
    cin >> quesTwo;
    cout <<"Who is your favorite celebrity?n Please enter all in one word.n";
    cin >> quesThree;
    cout <<"What is your favorite hobby?n";
    cin >> quesFour;
    cout <<"Which is your favorite quote?n";
    cin >> quesFive;
    cout << "Thank you for registering.";
    ofstream writer("Users.txt");
    writer << endl << endl << endl
    << "Full Name: " << fullName << endl
    << "Age: " << age << endl
    << "DOB: " << DoB << endl
    << "Answer to Question 1: "
    << quesOne<< endl
    << "Answer to Question 2: " << quesTwo << endl
    << "Answer to Question 3: " << quesThree << endl
    <<  "Answer to Question 4: " << quesFour << endl
    << "Answer to Question 5: " << quesFive << endl;
    writer.close();
    goto z;
    existUser:
    {
        string userName;
        char letter;
        cout << "Enter full username.n";
        cin >> userName;
        ifstream reader("Users.txt");
        if (! reader) {
            cout << "Error opening file.";
        } else {
            char(letter);
            for (int i = 0;! reader.eof(); i++) {
                reader.get(letter);
                cout << letter;
                reader.close();
            }
        }
    }
    z:
    return 0; 
}

"忽略这些可怕的代码"这个要求太过分了。在任何编程语言中都不要使用goto语句。当然,凡事都有例外,但这种情况很少见,而且这次也不例外。https://xkcd.com/292/

任何人都很难理解你的代码,但我将专注于你的直接问题,即在文本文件中找到一个单词。如果您的文本文件是非结构化的,并且不是特别大,那么最简单的方法可能是将其放入字符串中,并调用string的find方法。Getline在字符串头文件中。

你已经熟悉ifstream了。但是,您一个字符一个字符地阅读文件,这是非常慢的,如果您想搜索文件,上天会帮助您。您可以逐行读取,并将每行存储在字符串中。如果您使用文本文件,您可能会经常这样做。Getline从流中读取,直到遇到换行符,它丢弃换行符,将该行加载到字符串中,并将流移动到下一行的第一个字符。Getline的返回值有点奇怪,但足以说明您可以直接在while循环中使用它,当它到达文件末尾时它将停止。如果找不到,则返回std::string::npos,这是一个特殊值,表示各种字符串函数中某种类型的失败。

std::ifstream file("yourfile.txt");
std::string line; //this is the string that we'll be storing the line in
while (getline(file, line)) //read a line of text from file into our line variable
{
    if (line.find("text to find") != std::string::npos) //if we find our text in line
    {
        std::cout << "found it!n";
        break; //no need to read the other lines
    }
}

另一种方法,我可以这样做:

std::string line = "";
while (line.find("your text") == std::string::npos) //while I fail to find the line I'm looking for
{
     getline(file,line); // keep reading in lines
}
if (file.eof()) //if I read through the whole file without finding anything
    std::cout << "No luckn";
else
    std::cout << "found it!n";

第一个例子中的break是一种很常见的方法,对于较小的单循环,发生的情况很清楚。事实上,我敢打赌有些人会认为第一种方式更可取,因为它更简洁,更清晰。

一般来说,您只需要小心让代码到处乱跳即可。Gotos就是这样做的,如果你有大的嵌套循环,break也可以这样做。试图跟踪你的代码的人会遇到它,然后必须弄清楚内循环是什么,然后记住外循环中发生了什么。

下面是一小段代码,它将在文件中搜索关键字:

std::string text_read;
bool        text_found = false;
while (getline(reader, text_read))
{
  if (text_read == fullname)
  {
    text_found = true;
    break;
  }
}
if (text_found)
{
  cout << "Text found.n";
}