To read a string from text file (searching the string) and compare with user input

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

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";
				

		

			else if (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;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void read_file (ifstream& reader, string& search)
{
  string input = "" ;

  while (reader >> input)
  {
    if (input == search)
    {
      cout << "Found\n\n";
      return;
    }
  }
  cout << "Not found\n\n";
}

int main () {

  string search;
  ifstream reader ("Testing.txt");
  if (!reader)
  {
    perror ("\a");
    system ("pause");
    exit (-1);
  }

  cout << "Enter the string of a record you want to search: ";
  getline (cin, search);

  read_file (reader, search);
  
  system ("pause");
  return EXIT_SUCCESS;
}
Hello Thomas.....i have included the correction but the string from file still can not be found. What would be your next advise ?
How does your file look like? What do you search for?
Here below please

Last edited on
closed account (48T7M4Gy)
I think the question is what does a typical example of "testing.txt" look like? Perhaps if you posted the sample here. :)
Kemort, here is a section of the file:


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

closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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


http://www.cplusplus.com/reference/string/string/find/
Last edited on
What did you search for?

It finds NAIROBI for example
closed account (48T7M4Gy)
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.
Last edited on
eh ? kemort ! ....i real liked that ! Thanks Sir !
closed account (48T7M4Gy)
I'll add a few small changes to make it case-insensitive. Stand by. :)
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 ?
Last edited on
closed account (48T7M4Gy)
npos
http://www.cplusplus.com/reference/string/string/npos/

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 :) :)
Last edited on
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.
closed account (48T7M4Gy)
Excellent - good luck with it. Come back with your attempts if you need help.
Topic archived. No new replies allowed.