Use find function to search for a line in a text file

Hi all,

I'm trying to make a program that will search for a line in a text file using a non default delimitor at the start of the line. An example line in the text file would be as follows:

F Mary Smyth, 19, United Kingdom

I have been able to use the find function to search for and return the 'F' character but would like it to then display the whole corresponding line.

Is this possible with the find function?

or could you suggest another way of doing it?

Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
   ifstream readFromFile("data.txt");

   string Destinations[1] = {"F "};
   string data;

    while(!readFromFile.eof()){ 
        getline(readFromFile,data);

        for (int i = 0; i < 3; i++)
                if (data.find(Destinations[i])!=string::npos) cout << Destinations[i] << endl;
    }
If 'data' contains whole line and you want to show the whole line, then printing 'data' would make more sense than printing the short Destinations string.

If you do know that the thing you search for must be at the start of the line, then string::compare would sound potential:
http://www.cplusplus.com/reference/string/string/compare/
Thanks for reply will look into this now
Topic archived. No new replies allowed.