I want to design a programe that can search for a information in text file.

Hello as you can see am very new in this site, this is my first post... can somebody please help me out here. i want to design a program that can search for a file word or sentence on text file that i create before. can somebody please help me. i do not know how to do the source code for that. for e.g if i want to search Jobarteh on the text file i just need to type Jobarteh, and if it is found the result will be display.
basically this is the task i need to do;create a searching function for viewing or retrieving the information. this search should be related to the search keyword and suggestion list should be printed out if the keyword searched has been found in more than 1 record.
sorry i mean i already write on the file and save the file... now i want to search a word on the file to retrieve the file been save.
find_first_of

it's in the string class. http://www.cplusplus.com/reference/string/string/find/

All you have to do is read the file in 1 line at a time using getline(istream,string); and search the line using find_first_of or find, w/e

edit: PS. if your reading the same file you have written to, make sure that you close it after writing to it before you open it to read.
Last edited on
i think i gave you the right link.. just that you did not read it well.. it also shows how to read file..
thanks to you all... let me try to check that one.
#include <iostream>
#include <iomanip>
#include <cstring>
#include<windows.h>
#include <fstream>

using namespace std;
void colors ( int y );
//class declaration
class Patient_record
{
//public declaration
public:
string name,fatherName, motherName, address;
void personalinfo() //declaration of the function to get details of the record
{
cout<<"Name of the patient : ";
getline(cin,name);
cout<<endl;
cout<<"Father's name : ";
getline(cin,fatherName);
cout<<endl;

cout<<"Mother's name : ";
getline(cin,motherName);
cout<<endl;
cout<<"Address : ";
getline(cin,address);
cout<<endl;}

};

class Patient_record1 : public Patient_record
{
//public declaration
public:
};


class Patient_record2 : public Patient_record
{
//public declaration
public:
};


class Patient_record3 : public Patient_record
{
//public declaration
public:
};


class Patient_record4 : public Patient_record
{
//public declaration
public:
};


class Patient_record5 : public Patient_record
{
//public declaration
public:
};


class Patient_record6 : public Patient_record
{
//public declaration
public:
};


class Patient_record7 : public Patient_record
{
//public declaration
public:
};


class Patient_record8 : public Patient_record
{
//public declaration
public:
};

class Patient_record9 : public Patient_record
{
//public declaration
public:
};


int main()
{
// here i am going to call the patient data inorder to store the datas..
// i use file here to save the datas on the file...
// now i need to display everthing that i enter about the patient.
// inorder word here is where i do the finishing part of the program.
system("pause");
}

the program above is the one i designed in lab one... i do not write the complete source code but i hope u can understand the program. now this is the task that i need to complete.. please read it carefully and help me to complete the task. the question is this:
question: Using the hospital information system that you had developed in lab 1 (that is the program above), create a patient's searching function for viewing or retrieving patient's information. The search results should be related to the search keyword and suggestion list should be printed out if the keyword searched has been found in more that one patient's record.

please help me...
I get errors on all the lines where you use the command getline wrong, but the rest of your program compiles alright.
http://cplusplus.com/reference/iostream/istream/getline/

Also please try to enclose your code with the forums code tags. It makes it much easier to read for everyone. There should be a button you can click that has the symbols <> on it.
There's probably a better way of doing it rather than adding all the records to different classes, an array or vector of 1 class type would be much better.
yes Gcampton, but they ask us to use the concept of inheritance... but still O.k, can u show me how to do it... Kevinkj200 i compile all of it but there is nothing wrong wit getline... may be the compiler u are using is the problem. sorry may be i do not read that before but i will do it that way next time.
Well lets say you only have 9 records, I'm assuming that from what you have above:
create you class based on generic information you want it to hold:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Patient_Record 
{
  //public declaration
  public:
    Patient_Record(string name, int age, string patientNumber, string address, string history);
  private: 
    string name;
    int age;  // or DOB
    string patientNumber;
    string address;
    string history;

};


or something similar to above. then you just declare a vector of type Patient_Record.
if your reading this data from a file, then using a loop and getline, you can push new records into the vector by using the constructor of the class, or you can create your own methods to deal with how you handle data. Up to you...
Last edited on
thanks to you all.
Topic archived. No new replies allowed.