I want to search through a text file database and display if the word or string is within the text file...The code is doing but it doesn't find any word that actually exist. Help to correct. Here is the code snippet.
void read_file(ifstream& reader, string& search)
{
string reads;
while (! reader.eof ())
//for (unsigned int i=0; i<read.size (); i++)
reader.get (reads [i]); //-------i think the problem is here
if (reads !=search)
cout<<"Name " <<search<< " could not be found in the register\n";
elseif (reads ==search)
cout<<"Name "<<search<<" found in the register\n";
system ("pause");
return;
}
void main () {
string search;
ifstream reader ("Testing.txt", ios::in | ios::out);
cout<<"Enter the string of a record you want to search\n";
getline (cin, search);
read_file(reader, search);
reader.close ();
system ("pause");
EXIT_SUCCESS;
}
====================================================================================================================
Lr Num: 3w75768uty
Proprietor name: Elijah Mwangi
National Id Num: qwuitioyde
Proprietor address: eyoiyi
Kra PIN: eiouou
Date land was acquired: eyuu9iop
Date of title deed: ruoiiou
Category: reuiiuopu
Ownership type: ruiuiopi
County located: riuop[u
District: yuioopuop
Division: uropuipo
Location: jrioopuiop
Sub-location: siuoiuyoi
Village: uiyio
====================================================================================================================
Lr Num: TR657687970M
Proprietor name: MWANGI ELIJAH
National Id Num: 57688798
Proprietor address: 345-0200 KENYA
Kra PIN: T576686889L
Date land was acquired: 19.06.2016
Date of title deed: 19.06.2016
Category: PRIVATE
Ownership type: ABSOLUTE
County located: NAIROBI
District: KASARANI
Division: GITHURAI
Location: G44
Sub-location: SECTION 3
Village: FLYOVER
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string line = "";
std::string search = "";
std::string temp;
std::fstream inFile("search.txt");
std::cout << "Please enter search string: ";
std::cin >> search;
transform(search.begin(), search.end(), search.begin(), ::toupper);
if (inFile.is_open())
{
while (std::getline(inFile, line))
{
temp = line;
transform(temp.begin(), temp.end(), temp.begin(), ::toupper);
std::string::size_type found = temp.find(search);
if (found != std::string::npos)
std::cout << search << " found at: " << line << '\n';
}
}
else
std::cout << "File not opened\n";
inFile.close();
return 0;
}
Please enter search string: eLi
ELI found at: Proprietor name: Elijah Mwangi
ELI found at: Proprietor name: MWANGI ELIJAH
Program ended with exit code: 0
Please enter search string: NAIROBI
NAIROBI found at: County located: NAIROBI
Program ended with exit code: 0
This would probably need to be extended for regular expressions and data read in as a record, structure or class, or maybe simply as a data file line number. Ignore lines with "== ..." or every 15th and delimit rest by : character. Mwangi knows presumably.
Kindly nice fellas, would you please elaborate what the code is doing here:
1 2 3 4 5
if (found != std::string::npos) // role of npos ?? how come the code goes through even after found! =....
std::cout << search << " found at: " << line << '\n';
}
And if i was to search and display ELIJAH found at: Proprietor name: MWANGI ELIJAH , is it possible to iterate at the point that record is found and display also with of : Kra PIN: T576686889L .
Therefore :
1 2 3 4
ELIJAH found at: Proprietor name: MWANGI ELIJAH
of : Kra PIN: T576686889L
.... maybe by a line +2 for cursor to jump to second line of that record and display that Kra PIN: field ?
Regarding the ability to print out more detail once a match in a record is found, the short answer is yes. That's why I referred to classes (objects really) structs etc. It would not be all that hard, just read a group of lines in in some sort of a structured way, check for a match while ignoring the ===== lines. (You can include these lines actually.)
You might like to make it as an exercise for yourself. Somebody else here, even, might have a go :) :)
Thanks soo much Kemort.....your insight was great ....i will even try a struct object (and a function) to read the same file to expose details to a class object that implements less data hiding.