String turns up empty after find_last_of() and substr()?
<p>Self-teaching myself C++, and I know I'm missing something critical, but I can't for the life of me figure out what it is.</p>

<p>Forgive the huge block of code, I was tempted to trim it down to the critical elements, but I figured if I left it intact, you folks might have other educational criticisms about my coding style...</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;fstream&gt;

using namespace std;

//  main routine
int main(int argc, char *argv[]) {
    // will store filetype here for later
    string filetype = "";
    string filename;

    // if no arguments, die.
    if (argc &lt; 2) {
        cout &lt;&lt; "ERROR: Nothing to do." &lt;&lt; endl;
        return 1;
    }

    // if more than one argument, die.
    else if (argc &gt; 2) {
        // TODO: support for multiple files being checked would go here.
        cout &lt;&lt; "ERROR: Too many arguments." &lt;&lt; endl;
        return 1;
    }

    // otherwise, check filetype
    else {
        string filename = argv[1];
        cout &lt;&lt; "Filename: " &lt;&lt; filename &lt;&lt; endl;
        //searching from the end, find the extension of the filename
        int dot = filename.find_last_of('.');
        if (dot == -1){
            // TODO: Add support for filenames with no extension
            cout &lt;&lt; "ERROR: Filename with no extension." &lt;&lt; endl;
            return 1;
        }
        string extension = filename.substr(dot); 
        if (extension == ".htm" || extension == ".html"){
            filetype = "html";
        }
        else if (extension == ".c"){
            filetype = "c";
        }
        else if (extension == ".c++" || extension == ".cpp") {
            filetype = "cpp";
        }
        else {
            cout &lt;&lt; "ERROR: unsupported file extension" &lt;&lt; endl;
            // TODO: try to guess filetype from file headers here
        }
    }

    cout &lt;&lt; "Determined filetype: " &lt;&lt; filetype &lt;&lt; endl;
    cout &lt;&lt; "Filename: " &lt;&lt; filename &lt;&lt; endl;

    return 0;
}
                                            // All done :]
</code></pre>

<p>The issue I'm having is mysterious. I put the argument passed into a string like so:</p>

<pre><code>string filename = argv[1];
</code></pre>

<p>and then search it for an extension, starting from the end and working my way to the beginning:</p>

<pre><code>int dot = filename.find_last_of('.');
string extension = filename.substr(dot);
</code></pre>

<p>This all works as expected, but afterwards, when I try to output filename, it is mysteriously empty? I tried debugging with cout. When I print out the string BEFORE I search it, it prints properly. After, nothing prints. Like so:</p>

<pre><code>$ g++ test.cpp -o test.out; ./test.out foo.html
Filename: foo.html
Determined filetype: html
Filename:
</code></pre>

<p>I remembered something about iterators in the past, and tried using <code>filename.begin()</code> to reset it, but this did nothing. Can someone shed light onto this puzzling issue?</p>
