Searching a text file

I have a text file
1
2
3
Sohom~Bhattacharjee~7278453003~03326655594~soham.bhattacharjee15@gmail.com~|
Sayani~Roy~9833344187~03322341524~sayaniroy@gmail.com~|
Sinchan~Bhattacharjee~9088770954~03345555594~b.sinchan19@gmail.com~|



i have written this code.It works fine for the first contact but if we want to search the second or the third contact it is not able to find them...
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
  void Phonebook::search_name()
{
	int token;
	string field,line;
	char line1[50],line2[50];
	cout<<"Enter the first name of the person you wanna search ?? \n";
	cin.ignore();
	getline(cin,field);
	cout<<"person to be searched is "<<field;
    cin.get();
    token=0;
	ifstream readfil;
	readfil.open("data.txt");
	if(!readfil)
        {
            cout<<"file does not exist";
        }
    while(readfil.good())
    {

            readfil.getline(line1,12,'~');
            if(line1==field)
            {
                token=1;
                readfil.seekg(0,ios::beg);
                readfil.getline(line2,50,'|');
                cout<<"the person to be found is "<<line2;
                break;
            }
        if(token==0)
          cout<<"\n\n.....data not found...";
    }

}
Last edited on
Topic archived. No new replies allowed.