How to put string into a struct?

Hi :)
I have a problem with my code - the point of it is to save separately individiual parts of a .html file into struct.
For example, this is .html file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<html>
<head>
text text 123
</head>
<body>
<ul>
list
</ul>      
<p>
life life12356
</p>
<table> 
example table
</table>
</body>
</html>


As simple as it is, and i need to save everything for example between <table> and </table> into struct records.table.
The problem is, my code works only for the first example with <head>, but with the rest - it does not.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <fstream>
using namespace std;

const int row_length = 100;

 struct html_struct {
        string head;
        string body;
        string p;
        string table;
        string ul;
    };



int main()
{

html_struct records;
ifstream file("file.html");
string line;


while(getline(file, line))
{
    if( line == "<head>")
    {
    while(getline(file, line) && line!= "</head>" )
    records.head=line;
    }

    if( line == "<p>")
    {
    while(getline(file, line) && line!= "</p>" )
    records.p=line;
    }

    if( line == "<table>")
    {
    while(getline(file, line) && line!= "</table>" )
    records.table=line;
    }

    if( line == "<ul>")
    {
    while(getline(file, line) && line!= "</ul>" )
    records.ul=line;
    }
}

cout << records.head << endl;
cout << records.p << endl;
cout << records.table << endl;
cout << records.ul << endl;

file.close();
return 0;
}


If something is unclear I'll try to explain better.
Thanks for trying to help :)
it looks like you read all of the file looking for head, and then read the rest of the file looking for the others (but there is nothing left in the file now, you already read all of it).

instead it looks like you need this format:

1
2
3
4
5
6
7
while(blah.getline(..))
{
      if( line == '<head\>')
        do head stuff
     else if (line == '<more tags\>')
      etc
} //end while 

Topic archived. No new replies allowed.