[try Beta version]
Not logged in

 
How to get string from a file terminating with another string

Oct 21, 2008 at 8:41am
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 Oct 21, 2008 at 8:43am
Oct 21, 2008 at 9:17am
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.

Oct 21, 2008 at 6:24pm
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.
Oct 22, 2008 at 4:42am
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.
Oct 22, 2008 at 8:34am
It does if you want the second substring.
Oct 22, 2008 at 10:15am
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 Oct 22, 2008 at 10:27am
Oct 22, 2008 at 1:16pm
Oct 22, 2008 at 1:24pm
Ooops...Sorry...didn't know that
Oct 22, 2008 at 1:58pm
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.