HOW TO READ AND DISPLAY A SPECIFIC STRING FROM TEXT FILE AND DISPLAY TO SCREEN.

i would like this code to search through the textfile for data a user will specify at runtime. Then display to screen whether the string was found from the text file database. e.g. i have included some data contained in the text file and from which the program should search for "MWANGI ELIJAH" and "GJ4758869LT", then output whether those details can be found from the text file data base.

However the code is failing due to some bugs which am unable to resolve. Help review it please.


#include <iostream>
#include <fstream>
#include <string>
#include <limits>
#include <vector>

using namespace std;

int main (){

string searchOwnerName, landNumber, line;

int i, offset;


ifstream myFile;

myFile.open ("My_Land register.txt");//opening text file from which to search a specific string information

cout<<"Type the name you want to search here :\n";

getline (cin, searchOwnerName); //user input

cout<<"Enter the land number for the land owned by the above owner";

getline (cin, landNumber); //user input

if(myFile.is_open ())

{
while (!myFile.eof ())

{
getline (myFile, line);//reading strings from text file

for (i = 0; i<myFile.size ();i++)// i want to read anywhere from the My_Land register text file containing a data base strings

if ((offset = line.find (searchOwnerName, 0)) && (offset = line.find (landNumber,0) !=string::npos) //using find function to search for specific details from the text file database
{
cout<<"The owner name"<<searchOwnerName<<"\n and Land Number: \n"<<landNumber<<"has been found in My_Land register file."<<endl;
}
}


CONTENTS OF TEXT FILE:


LR UNIT NUMBER: RTI3866996L
NAME OF PROPRIETOR: MWANGI ELIJAH
NATIONAL ID CARD NUMBER: 4568786990
ADDRESS OF PROPRIETOR: BOX 234 NAIROBI
PIN NUMBER OF PROPRIETOR: B143233556367P
DATE_OF_ACQUISITION: 23-01-2016
DATE_OF_ISSUE_OF_TITLEDEED: 30-01-2016
CATEGORY: PRIVATE LAND
TYPE_OF_OWNERSHIP: ABSOLUTE
COUNTY_LOCATED: NAIROBI
DISTRICT_LOCATED: ROYSAMBU
DIVISION_LOCATED: GIRHURAI
LOCATION_LOCATED: SECTION 44
SUBLOCATION_LOCATED: WAIHENYA
VILLAGE_LOCATED: SONIC
------------------------------------------------------------------------------------------------------------
LR UNIT NUMBER: GJ4758869LT
NAME OF PROPRIETOR: TEST DATA
NATIONAL ID CARD NUMBER: 234536374
ADDRESS OF PROPRIETOR: KENYA
PIN NUMBER OF PROPRIETOR: RT474785859L
DATE_OF_ACQUISITION: TODAY
DATE_OF_ISSUE_OF_TITLEDEED: FUTURE DATE
CATEGORY: PUBLIC LAND
TYPE_OF_OWNERSHIP: LEASEHOLD
COUNTY_LOCATED: LAIKIPIA
DISTRICT_LOCATED: NARO MORU
DIVISION_LOCATED: EQUATOR
LOCATION_LOCATED: GWA KUNGU
SUBLOCATION_LOCATED: MUCII
VILLAGE_LOCATED: GWITU
------------------------------------------------------
Hi,

Please use code tags:

http://www.cplusplus.com/articles/z13hAqkS/


If you open a file, checked to see that it worked straight away, not some time later.

Don't loop on eof, loop on the the filestream itself.

if ((offset = line.find (searchOwnerName, 0)) && (offset = line.find (landNumber,0) !=string::npos)

Use == for comparison, = for assignment

Good Luck !!



Topic archived. No new replies allowed.