2017-11-25 9 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 
### 

내 쿼리가 각 범주에 대해 구체적으로해야합니다.

예를 들어, 세 개의 레코드가 있고 두 개가 같은 도시 "Oak Park"의 레코드 인 경우 두 개의 "Oak Park"레코드를 표시해야합니다. 나는이 문제를 해결하기 위해 시작 했어 아마도 이런을 "###" "***"에서 기록을 끌어 검색을하고 어떻게

:

{ 
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 = 
} 

그러나 나는 또한 통합해야 ("City :"+ search_token)을 검색하십시오.

그래서 (City Search) 할 때 그 도시에서 모든 티켓을 가져옵니다.

어떻게 구현 될까요? 올바른 방향으로 가고 있습니까?

내 작품은 지금까지 그냥 일반적인 검색 :

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. City에 대한 함수 호출을 다른 범주로 복제 할 것입니다. 나는 이미이 함수들을 호출하는 working switch 문을 가지고있다. –

답변

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

감사합니다. 어떻게 작동하는지 알려 드리겠습니다. –