0

In this example:

#include <iostream>
#include <fstream>
#include <string>
#include <deque>
#include <sstream>


struct tag {
    bool isOpening;
    std::string name;
};

void print(const std::deque<tag> &deq){
    for(const auto &i : deq){
        std::cout << i.name << std::endl;
    }
}

int main() {
//    std::ifstream infile("a.html");
//    std::stringstream buffer;
//    buffer << infile.rdbuf();
//    std::string buf = std::move(buffer.str());
    std::deque<tag> deq;
    std::string buf = "<html>\n"
                      "\n"
                      "<body>\n"
                      "    <h1>Hello <b>world</b></h1>\n"
                      "</body>\n"
                      "\n"
                      "</html>";

    size_t pos = 0;
    while ((pos = buf.find('<', pos)) != std::string::npos) {
        bool isOpening = true;
        if(buf[pos+1] == '/'){
            isOpening = false;
            pos++;
        }
        std::string token = buf.substr(pos, buf.find('>', pos));
        deq.push_back({isOpening, token});
        pos++;
    }

    print(deq);
}

I would like to parse for each tag its name and whether it is opening or not. But the output is:

<html
<body>
    <h
<h1>Hello <b>world</b>
<b>world</b></h1>
</body>

</ht
/b></h1>
</body>

</html>
/h1>
</body>

</html>
/body>

</html>
/html>

which is of course not correct. So what am I doing wrong?

1 Answer 1

1

the substr function in c++ takes in two arguments: The position, and the length of the substring, and not the start and end positions. So you're basically having almost all of the substrings be for the entire string, which is why you see so much repeated output. You're really just wanting to take that substr command and change it to

 buf.substr(pos, buf.find('>', pos)-pos-1);

Hope that works!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.