-1

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
    This needs more clarification. – Jongware Dec 24 '13 at 00:03
  • I don't understand: "`All of it is working except for when i try to edit a src tag`" and "`everything works except that part being written to the file.`" sound like a contradiction. What's the problem? – Andy Prowl Dec 24 '13 at 00:04
  • Your problem is probably related to the new src string that contains spaces. Spaces should be encoded %20 in URLs. Your question is not clear at all... – Stephane Lallemagne Dec 24 '13 at 00:11
  • Can you provide an example of what you expected to happen and what happened instead? – Scott Hunter Dec 24 '13 at 00:12
  • Don't ever say "everything works" if you're describing code that doesn't work. – Kerrek SB Dec 24 '13 at 00:18
  • You should probably describe 1. what exactly doesn't work, 2. show the piece of code you suspect isn't quite right. The code should be a [SSCCE](http://sscce.org/) for the problem (my experience is that I locate my problem creating an SSCCE in the first place, though). – Dietmar Kühl Dec 24 '13 at 00:46

1 Answers1

0

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;
}
Brandon
  • 22,723
  • 11
  • 93
  • 186
  • That won't work if there is more than one quotation mark following the first (after `src=`). – Captain Obvlious Dec 24 '13 at 05:55
  • Are you sure about that? You should try it. It finds the very first quotation mark and then the very last one. It does not care about what is between. It does work. – Brandon Dec 24 '13 at 06:09
  • If you parse `src="hello" tag="world"` which quotation mark do you think `find_last_of` is going to zero in on? Your code is flawed. – Captain Obvlious Dec 24 '13 at 06:14
  • LOL.. my code is flawed? How about your understanding of my code is flawed. I simply said it will replace anything inbetween the FIRST and the LAST quote. That is exactly what it does. http://ideone.com/ywBMeL Works as I said. It only cares what's in-between the first and the very last quote. So yes, the tag attribute is going to get eaten. You said `That won't work if there is more than one quotation mark following the first (after src=)`. Check your understanding again. – Brandon Dec 24 '13 at 06:16