-
Istringstream fail
In the following function, my istringstream sets the fail bit the second time through the while loop on the line with the HERE comment. I've checked the contents of both command and ist, and they both seem to be right. Any ideas about what I'm doing wrong? Under what conditions can the HERE line fail?
Code:
int openFile(string filename)
{
// open file
ifstream infile(filename.c_str());
if (!infile)
{
//handle error
}
string command;
istringstream ist;
//read in file contents
while (!infile.eof())
{
getline(infile,command);
ist.str(command);
ist >> command; //HERE
ist.str(ist.str().substr(command.length()+1));
//really long if-statement
}
return 0;
}
-
getline() may be failing are getting an empty string. See what is in command just before >> fails.
gg
-
I did. command contains the correct data. getline() is working as it should. I've made a semi-workaround...if ist>>command fails, I clear the fail bit and try again once. If it still fails, it throws an exception. Not exactly the way I wanted it to work. =/ It still doesn't explain why it's failing.
This is starting to happen throughout my program...ist>>anything is failing. Under what conditions does it fail?