将变量与txt文件中的每一行进行比较

Compare variable with each line from a txt file

本文关键字:一行 比较 变量 txt 文件      更新时间:2023-10-16

我必须将txt文件的每一行与用户输入变量进行比较。如果用户输入的单词存在于txt文件中,它应该提示用户"单词存在",如果没有,则退出程序。

这就是文本文件的样子:

hello
hey
wow
your

这是我的代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
    ifstream file("words.txt");
    string content;
    string userinput;
    while(file >> content) {
        cout << content << endl; // gets all the lines from the txt file
        while(userinput != "exit") {
            // asks user for input
            cin >> userinput;
            // compares two inputs 
            if (userinput == content)
            {
                cout << "The word exists." << endl;
            } else {
                break;
            }
            if (userinput == "exit") {
                break;
            }
        }
    }
    return 0;
}

这对我来说不起作用。我可以返回txt文件中的所有单词,但无法将用户输入的文本与txt文件的txt行进行比较。任何帮助都会很棒。谢谢

更新代码:

    while(iFile >> content) {
        while(userinput != "exit") {
            // asks user for input
            cin >> userinput;
            // compares two inputs 
            if (content.find(userinput) != std::string::npos)
            {
                cout << "The word exists." << endl;
            } else {
                break;
            }
            if (userinput == "exit") {
                break;
            }
        }
    }

p.S:我对c++还很陌生。学生

#include <string>
#include <iostream>
#include <fstream>
#include <unordered_set>
using namespace std;
int main()
{
    fstream file("wy.txt");
    string line;
    unordered_set<string> res;
    while(file>>line)
    {
        res.insert(line);
    }
    do 
    {
        cout<<"Please input the word: "<<endl;
        string str;
        cin>>str;
        if (res.find(str) != res.end())
            cout << "The word exists." << endl;
        else
            break;
    } while (true);
}

此代码可以正常工作。

您正在文件中的每一行标记上运行一个循环,在该循环中,您询问用户的猜测,直到他放弃每个猜测?不错,但不是你说的那样。

你想做什么:

  1. 读取文件,将其解析为保存在std::unordered_set中的单词标记
  2. 然后询问用户应该匹配哪个单词
  3. 要求std::unordered_map泄露其秘密

试试看。

在编写时:

// compares two inputs 
if (userinput == content)

您确实在检查用户是否在内容中输入了精确的文本。您想要的是检查用户输入是否包含在内容中:

// check if the userinput is found in the text
if(content.find(userinput) != std::string::npos)

您还需要阅读完整的文件。现在,每次向用户请求输入时,您都要读取输入文件。

#include <iostream>
#include <fstream>
#include <memory>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
inline static bool fileExists(const std::string& name) {
   struct stat buffer;
   return (stat (name.c_str(), &buffer) == 0);
}
/// Read the file content and return as a string
static std::string readFile(const std::string &filename)
{
   std::ifstream t(filename);
   std::stringstream buffer;
   buffer << t.rdbuf();
   return buffer.str();
}

int main() {
    std::string filename("words.txt");
    if(!fileExists(filename)) 
       // print error
       return -1;
    string content = readFile(filename);
    string userinput;
    ... // handle userinput from now

请注意,这是低效的。由于您的文本总是相同的,并且您正在重复搜索,因此可以对其进行预处理。有多种数据结构可以提供帮助。例如,您可以有一个散列映射,并用每一行作为关键字填充它。