Thread: Istringstream fail

  1. #1
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401

    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;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    getline() may be failing are getting an empty string. See what is in command just before >> fails.

    gg

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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?
    Last edited by pianorain; 03-10-2003 at 02:23 AM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. bad and fail of steam
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 02-19-2008, 03:07 AM
  3. istringstream problem in switch block?
    By wolfindark in forum C++ Programming
    Replies: 4
    Last Post: 06-24-2007, 11:56 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. Initializing an istringstream
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2006, 08:49 PM