Before you extract a sub-string, you need to check that the range of the sub-string is physical (that is, the first index of a call to $substr$ comes before the last). I suspect that your file contains an empty line, in this case a call to $find_first_not_of$ will return $npos$ indicating that nothing was found before the end of the string.
I would suggest adding a check for $npos$ returned from $find_first_not_of$:
// Strip leading spaces
sos = mystring.find_first_not_of(" ");
/* sos == 0 would mean substr(0, npos) -> useless.
sos == npos would mean substr(npos, npos) -> un-physical. */
if (sos != 0 && sos != string::npos)
{
mystring = mystring.substr(sos, string::npos);
}
// Split string by first space
sos = mystring.find_first_of(" ");
substring1 = mystring.substr(0, sos); /* sos != 0, since strip leading spaces */
/* Check that sos+1 is inside the string. */
if (sos+1 < mystring.length())
{
substring2 = mystring.substr(sos+1, string::npos);
}
else
{
substring2 = "";
}
sos = substring2.find_first_of(" ");
/* sos == 0 would cause substr(0, 0) -> un-physical,
sos == npos would cause substr(0, npos) -> useless. */
if (sos != 0 && sos != string::npos)
{
substring2 = substring2.substr(0, sos);
}
count << substring2 << ' ' << substring1 << std::endl;