2017-11-25 223 views
-1

我正在处理一些代码,它将从文本文件中搜索故障单。每张票都以“***”开头,以“###”结尾。在特定字段中的关键字搜索之后从文本文件中提取多个记录

(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; 
    } 
} 
+0

“我的查询必须针对每个类别。” - 按类别划分您的票号,名字,牌子,城市等等? –

+0

@JackOfBlades正好。我只是将城市的功能调用复制到其他类别。我已经有一个工作开关语句调用这些函数。 –

回答

0

我想你宁愿做一个普通的功能为他们所有。如果我理解你最初计划的是正确的,那么开关意味着你描述的情况会有更多的开销。

你宁愿做的是这样一个类似于:

//This function compares the ticket's category's contents with the given string 
std::string checkString(std::string stringToCheck) { 
    std::string contentDelimiter = " : "; //Text after this will be read in 
    std::string content = stringToCheck.substr(stringToCheck.find(contentDelimiter) + 
    contentDelimiter.length(), stringToCheck.length()); 
    //Find where the end of " : " is, read from there 'til end of line into "content" 
    return content; 
} 

//This function opens the file, compares sought info to each relevant column, 
//and types matching tickets out 
void searchFile(const std::string& filePath, const int& amountOfCategories, 
        const std::string& matchInfo, const int& categoryIndex) { 
    int i = 0; 
    std::string tempTicket[amountOfCategories]; 
    //Make a template to copy each ticket to for later use 

    std::ifstream inFile; 
    inFile.open(filePath.c_str()); 
    while (!inFile.eof()) { //Go through entire file 
     std::getline(inFile, tempTicket[i]); //Put next line into the relevant slot 
     i++; 
     if (i == amountOfCategories) { 
      if (matchInfo == checkString(tempTicket[categoryIndex]) { 
       for (int j = 0; j < amountOfCategories; j++) { 
         //If you want it to skip the *** and the ###, 
         //you change it to this: int j = 1; j < amountOfCategories-1; 
        std::cout << tempTicket[j] << std::endl; 
       } 
       std::cout << std::endl; 
      } 
      i = 0; 
     } 
    } 
    inFile.close(); 
} 

而且你必须使函数查找类别指标和类别的静态字符串数组,循环他们通过搜索时为正确的索引。类似这样的:

//This outside of main obviously as an independent function 
int position(const std::string& soughtCategory, 
       const std::string arr[], const int& amountOfCategories) { 
    int found = -1; 
    for (int i = 0; i<amountOfCategories; i++) { 
     if (arr[i] == soughtCategory) { 
      found = i; 
     } 
    } 
    return found; 
} 

//This in main: 
int amountOfCategories = 10; //Or however many there will be(no duplicates) 
std::string categories[amountOfCategories]; 
//categories[0] = "***"; 
//Place all of the others between these manually 
//categories[9] = "###"; 

//Then call it like this, for example, in main: 
searchFile("file.txt", amountOfCategories, "Tampa", position("City", categories, amountOfCategories)); 
+0

谢谢,我会告诉你它是如何工作的。 –

相关问题