Problem with reading files

Hi everyone,
I have a task to write a program which finds specific words in a simple html file and does some other stuff with them (the file contains inside <body> only tags).
My problem for now is, why my program does not find the <body> word in a file.

Is is a proper way to open a file with a function, or is smth wrong with my idea? I'm out of ideas what's wrong...

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


void OpenFile(ifstream &f)
{
    char file_name[50];
    cout << "Name of the file to open: ";
    cin >> file_name;
    f.open(file_name, ios::in);

    while(!f)
        {
        cerr << "Error with opening: " << file_name << endl;
        cin >> file_name;
        f.open(file_name, ios::in);
        }
}


int GetBodyPosition(ifstream &f)
{
char ch;
string is_body = "";
int pos = -1;

    while(f.get(ch))
    {
        if(ch == '<')
        {
            while(f.get(ch) && ch != '>')
            {
            is_body += ch;
                if(is_body=="body")
                {
                pos = (int) f.tellg() + 1; 
                return pos;
                }
            }
            is_body = "";
        }
   }
   return pos;
}

void SaveToBody(int BodyEndPos, ifstream &fIn, ofstream &fOut)
{
    int curPos = fIn.tellg(); 
    fIn.seekg(0); 
    char ch;

    while (fIn.tellg() != BodyEndPos)
    {
        fIn.get(ch);
        fOut<<ch;
    }

    fIn.seekg(curPos);
}

int main()
{
    ifstream in_file;
    ofstream out_file;

    OpenFile(in_file);

    int BodyEndPos = GetBodyPosition(in_file);
    if(BodyEndPos == -1)
    {
        cerr << "Error: body not found" << endl;
        return -1;
    }

    SaveToBody(BodyEndPos, in_file, out_file);

    cout << GetBodyPosition(in_file);  

    in_file.close();
    return 0;
}


And here is a simple html file example I use:

1
2
3
4
5
6
7
8
<html>
<head></head>
<body>
    <ul>ulllul</ul>
    <p>srarara</p>
    <table>taaable123</table>
</body>
</html>


Thanks a lot for trying to help

Last edited on
What makes you think it isn't working?
The original value returned by your function is 27, which seems correct.
But of course it doesn't find the body tag on the second call to the function since you haven't rewound the file.

It can be simplified:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int findBodyTag(ifstream &f)
{
    int i = 0;
    for (char ch; f.get(ch); )
    {
        if (ch == "<body>"[i])
        {
            if (++i == 6)
                return f.tellg();
        }
        else
            i = 0;
    }
    return -1;
}

Or generalized:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int findTag(ifstream &f, string tag)
{
    int i = 0;
    for (char ch; f.get(ch); )
    {
        if (ch == tag[i])
        {
            if (++i == int(tag.size()))
                return f.tellg();
        }
        else
            i = 0;
    }
    return -1;
}

These are returning the position after the end of the tag, of course, but that seems to be what you are looking for.
Last edited on
@dutch,
thank you for your answer.
I have a question, could you explain me what those lines do?

1
2
3
    for (char ch; f.get(ch); )
    {
        if (ch == "<body>"[i])


How can I check if a char variable is "<body>"? I don't understand it, probably it's connected with this [i] but I've never seen or used this combination so I would be very grateful if you could explain this to me.

Also, when I changed the function looking for BodyTag as you adviced, it still returns in my program -1, as if there were no body tags.

I just added at the end of main(), but before closing the file line:

 
cout << GetBodyPosition(in_file);


and it returns -1 every single time... But as you said, it should return 27...
I don't know what is wrong or what I do not understand.

If you or someone could look at my problem once more, I would be more than pleased.
Help is still needed.

I tried adding in function GetBodyPosition after Line 43 or 44 this:
 
f.seekg(0, ios::beg);


But it didnt change anything, program still returns - 1 :/
Topic archived. No new replies allowed.