string tag processing

Heylo,
I am trying to write a search function that takes in a file formatted like html tags with only text between tags.

depending on the function that calls this search function, it would cout the appropriate text between the appropriate tags.

I do not believe this to be difficult but im fairly new at string library and have no idea how to do this... T_T

thanks
so far i have this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void read(string tag, char* file){
	string parser;
	string data;
	ifstream in(file);
	while(!in.fail()){
		in>>parser;
		if (parser == "<"+tag+">"){
			in>>data;
			while(!in.fail() && data!="</"+tag+">"){		
				cout<<data;
				in>>data;
			}
		}
	}
}

int main(){
	read("tag1", "data");
}


but the output will remove the whitespaces (because of in>>data)...
how do i solve this?
is there a better implementation than mine?
So you're trying to preserve the whitespaces? I would suggest the use of getline(in,data); and a small adjustment to your search function to actively search for tags within the given data. Hopefully, you don't mind too much if the newlines get dropped. If you do, then try reading the whole file in character by character and searching for tags while this happens. :)

-Albatross
Last edited on
wow brillant, didnt even think of that! thx!!
Topic archived. No new replies allowed.