searching in a file

HI guys,

I have a difficulty with searching in a multi line file. this is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  void search_profile()
{
    system("cls");
    int id;
    int counter=0;
    string read_line;
    cout<<"SEARCHING A PATIENT PROFILE: \n";
    cout<<"Enter User id: ";
    cin>>id;
    ifstream file;
    file.open("Patients.txt");
    while(!file.eof())
    {
    getline(file, read_line);
    counter++;
    if(read_line.find(id)!=string::npos)
    {
        cout<<"Data found at line: "<<counter<<endl;
        cout<<read_line<<endl;
        break;
    }
    }
    file.close();
    }


each line of the file has a unique id that i want to do the search based on that id, if the id is found the total line to be displayed. but this function only works well with the first line, so if i search something in the second or other lines it will not find them.

$ grep --line-number --max-count=1 id file


std::string id;
Thanks buddy ;)
Topic archived. No new replies allowed.