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

时间:2015-07-19 15:54:01

标签: c++

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

如果之前已经回答过这样的事情,请道歉。找不到它。

代码:

#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/E\n";
    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/YYYY\n";
    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; 
}

2 个答案:

答案 0 :(得分:1)

“忽略可怕的代码”太过分了。不要以任何编程语言使用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 luck\n";
else
    std::cout << "found it!\n";

第一个例子中的中断是看到它完成的一种非常常见的方式,对于较小的单循环,很明显会发生什么。事实上,我敢打赌,有些人会认为第一种更为可取,因为它更简洁,更清晰。

一般情况下,您只需要小心使代码跳转到处。 Gotos就是这么做的,如果你有大的嵌套循环,休息可以做到这一点。有人试图按照你的代码遇到它,然后必须弄清楚封闭循环是什么,然后记住外循环中发生了什么。

答案 1 :(得分:0)

这是一个小片段,可以在文件中搜索关键字:

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";
}
相关问题