C++ editing a src=" " tag in html

517 views Asked by At

I am trying to use c++ to edit a large amount of html files. All of it is working except for when i try to edit a src tag. I think it is because of the quotation marks. Here is my code.

string strReplace2 = "src=\"\""; //string to replace

and

strTemp = "src=\"http://localhost/Media/TV Shows/The Big Bang Theory Season 6/" + filename + "\"";

When i run the program everything works except that part being written to the file.

1

There are 1 answers

4
Brandon On

You can try this for replacing things in-between quotes..

void replace_between_quotes(std::string &str, const std::string &replacement)
{
    std::size_t pos_f = str.find_first_of("\"");
    std::size_t pos_e = str.find_last_of("\"");

    if (pos_f < pos_e && pos_f != std::string::npos && pos_e != std::string::npos)
    {
        std::size_t len = pos_e - pos_f - 1;
        str.replace(pos_f + 1, len, replacement);
    }
}

int main()
{
    std::string filename = "somefile";
    std::string str = "src=\"http://localhost/Media/TV Shows/The Big Bang Theory Season 6/" + filename + "\"";
    replace_between_quotes(str, filename);
    std::cout<<str;
}