-3

I am trying to parse an .html file to find a certain tag and then starting from the position I reached write into the file:

std::fstream file;
file.open(".\\img\\file.html", std::fstream::in || std::fstream::out);

if (file.is_open())
{
    char s[1024];
    bool f = false;
    while(f != true)
    {
        file.getline(s,1024);
        if (strstr(s,"<table>") != NULL)
            f = true;
    }

    file << "Something";
}
else
    printf("Error opening file.html\n");

from the debug I can confirm i find the desired tag, but nothing is written to the file, what am I doing wrong?

  • Make life easier with [`libxml++`](http://libxmlplusplus.sourceforge.net/docs/manual/html/) – P0W Oct 11 '13 at 16:26
  • Thanks for the tip, but my question is not about the parsing. My problem is that I'm unable to write into the .html file. – user2837795 Oct 11 '13 at 16:33
  • 1
    you can't just go to some part of an XML file and insert an element, expecting the remaining content to magically be shifted to to make space using basic stream io. You need to be using an XML toolkit for this. [See this question about how to choose the right one for your needs](http://stackoverflow.com/questions/9387610/what-xml-parser-should-i-use-in-c/9387612#9387612). – WhozCraig Oct 11 '13 at 16:49

1 Answers1

2
file.open(".\\img\\file.html", std::fstream::in || std::fstream::out);

should be

file.open(".\\img\\file.html", std::fstream::in | std::fstream::out);

you don't want the logical operator or || but the bitwise or |

But as the other commentators said, using the << operator will simply overwrite that part of your file, if you want to insert something you need to copy everything after the insertion point. It's probably best to either use a library like suggested or create a temporary file and alter the original after all modifications are done.

PeterT
  • 7,981
  • 1
  • 26
  • 34