string::find returning a value of -1 when it's expected to return a value of 0 [C++] (Updated)
<p>I have the following code that doesn't work:</p>

<pre><code>string line;
string line_sub;
size_t open_tag_start;
const string open_tag = "&lt;image&gt;";
const int open_len = open_tag.length() + 1;

open_tag_start = line.find(open_tag);
line_sub = line.substr(open_tag_start, open_len);
</code></pre>

<p>When I try to run this code, I get the following error:</p>

<pre><code>terminate called after throwing an instance of 'std::out_of_range'
what():  basic_string::substr
Aborted (core dumped)
</code></pre>

<p>I've figured out that this error is occurring because the <code>line.find</code> line is returning a value of <code>-1</code> to the variable <code>open_tag_start</code>. I can rectify the problem by hard coding a value of <code>0</code> into the variable <code>open_tag_start</code>, but I need this to a generic algorithm that will be able to find the tag at any point in the line, so it has to be a variable. Can anyone see what I'm doing wrong here?</p>

<hr>

<p><strong>Update</strong>: I worded my original question wrong. Sorry about that. Here's some more info.</p>

<p>My goal with this code if to extract a <code>string line_sub</code> from <code>string line</code>, which does indeed contain a string, and when I set <code>size_t open_tag_start = 0</code>, I am able to compile and execute the code and observe the expected output. <code>line</code> is not empty and my problem is that when I replace</p>

<pre><code>line_sub = line.substr(open_tag_start, open_len);
</code></pre>

<p>with </p>

<pre><code>line_sub = line.substr(0, open_len);
</code></pre>

<p>my problem goes away and I can compile and execute the code. </p>

<hr>

<p><strong>Update 2</strong>: This is a short version of my program that contains only the parts that are causing the problems. Attempting to compile this code will yield the error message detailed above. The file <code>rss.xml</code> is the RSS feed for engadget.com <a href="http://www.engadget.com/rss.xml" rel="nofollow">http://www.engadget.com/rss.xml</a></p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;fstream&gt;
#include &lt;cstring&gt;
using namespace std;

void get_tag_contents(ifstream&amp; rssfile, string line, string open_tag);

int main()
{
    const string open_tag = "&lt;image&gt;";

    ifstream rssfile;
    rssfile.open("rss.xml");
    string line;

    getline(rssfile, line, '\n');
    get_tag_contents(rssfile, line, open_tag);

    return 0;
}

void get_tag_contents(ifstream&amp; rssfile, string line, string open_tag)
{   
    const int open_len = open_tag.length() + 1;
    size_t open_tag_start;
    string line_sub;

    open_tag_start = line.find(open_tag);
    line_sub = line.substr(open_tag_start, open_len);
}
</code></pre>
