字符串与文件数据的比较

Comparison of string with the File data

本文关键字:比较 数据 文件 字符串      更新时间:2023-10-16

我目前陷入了一个非常简单的问题,这段代码是我的程序的一部分,它从用户获取数据,然后从文件(包含电影和演员的名称)中读取数据,并将其与输入进行比较,但它给出了错误的结果。这是我的代码

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    string name;
    getline(cin, name);
    string compare;
    ifstream myfile;
    myfile.open("movie.txt");
    bool found = false;
    if (myfile.is_open())
    {
        while (getline(myfile, compare))
        {
            if (compare == name)
            {
                cout << "record found" << endl;
                found = true;
            }
        }
        myfile.close();
    }
    if (found)
        cout << "not found" << endl;
}

任何关于这方面的帮助将是感激的。由于

对于调试,您可以从这里在comparename上使用string_to_hex