How to get string from a file terminating with another string

I want to get a string from file terminating with another string. As

We have data file data.txt and the text in it is -

kkasdasjdnajsdnjasdasd#kjlkldasfkldsklfdsklfk##skjaskjdajdjasdasdjkas##

i want to get string from this file ending with "##".

Please provide me the code that i can use for this purpose in c++. (fstream)

Last edited on
You will need to read the stream character by character and check for last two characters read = ##.

See http://www.cplusplus.com/doc/tutorial/files.html for info on file I/O.

You could try the getline function from the string library:
1
2
3
4
5
6
7
8
string buff;
ifstream inf;

do
{
    getline(inf, buff, '#');
} while (inf.peek()!='#');
//buff should now hold the string you want 


This will get the FIRST string ending in '##', ie the second one you have there.
cjmalloy, the code you have written is not giving me the right answer. it stops, while executing it. please check and provide me the correct code.
It does if you want the second substring.
http://www.cplusplus.com/reference/iostream/istream/getline.html
Here u have the declaration of getline();....The second parameter of that function cannot be a string, it must be an int.

Faldrax said exactly what u should do and there are many ways to do it.
Last edited on
Ooops...Sorry...didn't know that
Hey aakashjohari,

You're on a forum belonging to one of the best C++ reference sites on the net. Read this http://www.cplusplus.com/reference/iostream/istream/

And figure it out.

Thanks,
D
Topic archived. No new replies allowed.