通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?
What is the most easiest way to search in a txt file by comparing the lines in C++?
我正在编写一个代码,从电影名称的文本文件中搜索有关任何电影的信息。关于电影的信息有几行,我必须将它们打印在屏幕上。 我尝试阅读其他内容,但无法理解它们。 我也不喜欢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 左右,这也是一件好事。
相关文章:
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 在OSX上使用CMake将Adobe的XMP工具包构建为共享库的最简单方法是什么?
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?
- 请求最简单的 OpenMP 目标 GPU 示例
- 将时间戳打印到流的最简单方法
- 在C++中创建文件夹选取器对话框的最简单方法是什么?
- C++20 个模块在 clang (Windows):在最简单的示例中键入信息错误
- C++ lower_bound() 搜索最接近目标值的元素
- 对多个(可能)重叠范围进行分区的最简单算法
- 在 cpp 中最简单的图形实现
- boost::variant - 对变体应用算术的最简单方法
- 多线程 gtkmm 应用程序最简单的示例
- 创建可以遍历 std::map 值的通用模板迭代器的最简单方法是什么?
- 在排序数组中搜索最接近的 int 值 <= 从右起给定值
- 在C++中将算术类型转换为 std::array 的最简单方法<uint8_t>
- 标准库容器最简单、性能差的哈希类是什么?
- C++-将ASCII字符数组中可能的数值转换为字符的最简单方法
- 无法获得最简单的JNI足以在Windows 10上运行
- 从 txt 文件中读取数据的最简单方法
- 如何以最佳、最简单的方式在文件中搜索