通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?

What is the most easiest way to search in a txt file by comparing the lines in C++?

本文关键字:搜索 最简单 方法 是什么 文件 比较 C++ txt      更新时间:2023-10-16

我正在编写一个代码,从电影名称的文本文件中搜索有关任何电影的信息。关于电影的信息有几行,我必须将它们打印在屏幕上。 我尝试阅读其他内容,但无法理解它们。 我也不喜欢Do/While循环,我更喜欢使用For 循环。我尝试使用find((,但无法让它工作。我在这里完全是一个初学者。并且if/else部分中的部分不起作用。我不知道如何比较这些线条。另外,除了基本的东西之外,我知之甚少,所以很可能我不会知道你告诉我的任何事情。

bool search() // The search option to search in the txt file
{
string choi;
bool mainFlag = false;
string line = "";
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
reSrch:
string name = "n";
int year = 0;
string star = "n";
string blunt;
string blunt2;
int blunt3;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
cout << "n Please enter the name of the Movie (or n to skip name) : ";
cin >> blunt;
if (blunt == "n")
blunt = "n";
else
name = blunt;
cout << "n Please enter a name of any one star of the Movie (or n to skip name of star) : ";
cin >> blunt2;
if (blunt2 == "n")
blunt2 = "n";
else
star = blunt2;
cout << "n Please enter the year of production of the Movie (or 0 to skip year) : ";
cin >> blunt3;
if (blunt3 == 0)
blunt3 = 0;
else
year = blunt3;
////////////////////////////////////////////////////////////////////////////////////////////
if (blunt == "n" && blunt2 == "n" && blunt3 == 0)
{
cout << "n You have not entered any of the information to make a search. Would youn like to try again? If not then you will be sent back to the main menu."
"nn Please enter yes or no : ";
cin >> choi;
if (choi == "YES" || choi == "Yes" || choi == "yes")
{
Sleep(1500);
system("CLS");
goto reSrch;
}
else
{
Sleep(1500);
system("CLS");
//mainFlag = true;
}
}
else
{
if (name != "n")
{
if (star != "n")
{
if (year != 0)
{
mov.open("movie.txt");

for (unsigned int curLine = 0; getline(mov, line); curLine++) 
{ 
if (line.find(name, 0) != string::npos)//npos is used to tell = no matches found
{
cout << "found: " << search << "line: " << curLine << endl;
}
}
for (unsigned int curLine = 0; getline(mov, line); curLine++)
{
if (line.find(star, 0) != string::npos)
{
cout << "found: " << search << "line: " << curLine << endl;
}
}
for (unsigned int curLine = 0; getline(mov, line); curLine++)
{
if (line.find(year, 0) != string::npos)
{
cout << "found: " << search << "line: " << curLine << endl;
}
}
mov.close();
}
else
{
mov.open("movie.txt", ios::app);
for (unsigned int curLine = 0; getline(mov, line); curLine++)
{
if (line.find(name, 0) != string::npos)
{
cout << "found: " << search << "line: " << curLine << endl;
}
}
for (unsigned int curLine = 0; getline(mov, line); curLine++)
{
if (line.find(star, 0) != string::npos)
{
cout << "found: " << search << "line: " << curLine << endl;
}
}
mov.close();
}
}
else
{
if (year != 0)
{
mov.open("movie.txt", ios::app);
for (unsigned int curLine = 0; getline(mov, line); curLine++)
{
if (line.find(name, 0) != string::npos)
{
cout << "found: " << search << "line: " << curLine << endl;
}
}
for (unsigned int curLine = 0; getline(mov, line); curLine++)
{
if (line.find(year, 0) != string::npos)
{
cout << "found: " << search << "line: " << curLine << endl;
}
}
mov.close();
}
else
{
mov.open("movie.txt", ios::app);
for (unsigned int curLine = 0; getline(mov, line); curLine++)
{
if (line.find(name, 0) != string::npos)
{
cout << "found: " << search << "line: " << curLine << endl;
}
}
mov.close();
}
}
}
else
{
if (star != "n")
{
if (year != 0)
{
mov.open("movie.txt", ios::app);
for (unsigned int curLine = 0; getline(mov, line); curLine++)
{
if (line.find(star, 0) != string::npos)
{
cout << "found: " << search << "line: " << curLine << endl;
}
}
for (unsigned int curLine = 0; getline(mov, line); curLine++)
{
if (line.find(year, 0) != string::npos)
{
cout << "found: " << search << "line: " << curLine << endl;
}
}
mov.close();
}
else
{
mov.open("movie.txt", ios::app);
for (unsigned int curLine = 0; getline(mov, line); curLine++)
{
if (line.find(star, 0) != string::npos)
{
cout << "found: " << search << "line: " << curLine << endl;
}
}
mov.close();
}
}
else
{
if (year != 0)
{
mov.open("movie.txt", ios::app);
for (unsigned int curLine = 0; getline(mov, line); curLine++)
{
if (line.find(year, 0) != string::npos)
{
cout << "found: " << search << "line: " << curLine << endl;
}
}
mov.close();
}
}
}
}

return mainFlag;
}

你的逻辑是错误的。您的代码假设您可以针对每种不同的情况多次读取文件,但事实并非如此。您必须读取文件一次,然后在读取文件时对每一行应用适当的逻辑。

另外year是一个整数,你不能用find()来寻找整数,你需要先将整数转换为字符串,我为此使用to_string函数。

像这样的东西

string year_string = to_string(year);
for (unsigned int curLine = 0; getline(mov, line); curLine++) 
{
bool name_ok = name == "n" || line.find(name) != string::npos;
bool star_ok = star == "n" || line.find(star) != string::npos;
bool year_ok = year == 0 || line.find(year_string) != string::npos;
if (name_ok && star_ok && year_ok)
{
cout << "found: " << search << "line: " << curLine << endl;
}
}

如果该类别中没有提供任何搜索,或者如果为搜索提供了某些内容并找到了它,则_ok变量为真。如果所有_ok变量都为真,那么我们找到了要查找的内容。

这段代码也是你拥有的代码长度的 1/10 左右,这也是一件好事。