title: Getting error during runtime related to file input using getline
body: <p>This is not homework, it is self study. I am getting an unexpected error that is think is the result of getline requesting after the end of file.  I though I was checking to see if input was successful with the while(getline(inf,mystring)) but its not working. How do I effectively check for end of file if this is not the case?</p>
  
  <p>This is my code</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  int main(int argc, char** argv)
  {
      string mystring, substring1 = "", substring2 = "";
      int sos;
      ifstream inf (argv[1]);  //open file for reading
      if (!inf)
      {
          // Print an error and exit
          cerr &lt;&lt; "Uh oh, " &lt;&lt; argv[1] &lt;&lt; " could not be opened for reading!" &lt;&lt; endl;
          exit(1);
      }
          while(getline(inf,mystring))
          {
                  sos = mystring.find_first_not_of(" ");
                  if (sos != 0)
                  {
                      mystring = mystring.substr(sos, string::npos);
                  }
                  sos = mystring.find_first_of(" ");
                  if (sos != 0)
                  {
                      substring1 = mystring.substr(0,sos);
                      substring2 = mystring.substr(sos + 1, string::npos);
                  }
                  sos = substring2.find_first_of(" ");
                  if (sos != 0)
                  {
                      substring2 = substring2.substr(0, sos);
                  }
                  cout &lt;&lt; substring2 &lt;&lt; " " &lt;&lt; substring1;
  
          }
      return 0;
  }
  </code></pre>
  
  <p>This is the error</p>
  
  <pre><code>World Helloterminate called after throwing an instance of 'std::out_of_range'
    what():  basic_string::substr
  </code></pre>
  
  <p>This is the input file input.in</p>
  
  <pre><code>         Hello World
  </code></pre>