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?
This is my code
#include <iostream>
#include <string>
#include <fstream>
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 << "Uh oh, " << argv[1] << " could not be opened for reading!" << 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 << substring2 << " " << substring1;
}
return 0;
}
This is the error
World Helloterminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr
This is the input file input.in
Hello World