我正在处理一些代码,它将从文本文件中搜索故障单。每张票都以“***”开头,以“###”结尾。在特定字段中的关键字搜索之后从文本文件中提取多个记录
(1)从“城市”字段中的用户输入中搜索关键字。 (2)返回记录中的所有记录和行,一旦找到,而不仅仅是一个。
的记录是这样的:
***
Ticket Number :
First Name : T
First Name : C
Address of Violations : 123 Malberry Ln.
City : Oak Park //Need user to input City and Pull all tickets from that City
Plate : Q1234
Vin Number : V1234
Violation Narrative : NO PARKING
Violation Code :V1234
Violation Amount :50
Late Fee : 100
Make :
Model :
Was this Paid ? : 0
Date : 1012017
Time : 1200
Pay by Date : 1012018
Officer Number : 6630
###
我的查询必须针对每个类别。
例如,如果有三条记录,两条来自同一城市“橡树园”,我希望显示两条“橡树园”记录,而不仅仅是一条。
我如何开始工作了这一点做,从“***”到“###”可能像这样拉记录搜索:
{
ifstream in("tickets.txt");
string beforeEqual = "";
string afterEqual = "";
while (!in.eof())
{
getline(in, beforeEqual, '***'); //grtting string upto =
getline(in, afterEqual, '###'); //getting string after =
cout << afterEqual << endl; //printing string after =
}
但我也需要纳入搜索(“城市:”+ search_token)。
所以当我做一个(城市搜索)它从该城市拉出所有门票。
这将如何实施,我在正确的轨道上?
我的工作,到目前为止,只是一个通用搜索:
void city_search() {
string search;
string line;
ifstream inFile;
inFile.open("tickets.txt");
if (!inFile) {
cout << "Unable to open file" << endl;
exit(1);
}
cout << "Please enter a plate Number to search : " << endl;
cin >> search;
size_t pos;
while (inFile.good())
{
getline(inFile, line); // get line from file
pos = line.find(search); // search
if (pos != string::npos) // string::npos is returned if string is not found
{
cout << search << "Found!";
break;
}
}
“我的查询必须针对每个类别。” - 按类别划分您的票号,名字,牌子,城市等等? –
@JackOfBlades正好。我只是将城市的功能调用复制到其他类别。我已经有一个工作开关语句调用这些函数。 –