istream zurückspulen



  • hi.
    wie komme ich bei einem istream an eine vorher gelesene stelle zurückkehren, bzw. warum funktioniert das nicht:

    bool read_word( ::std::istream & i, ::std::string &word )
    {
        return  i >> word;
    }
    
    [...]
    
    //--------------------
    //in einer anderen funktion:
    
    ::std::streampos old_position = i.tellg(); //savegame
    
    //count number of coordinates:
    int j = 0;
    while( read_word(i,word) && word != "end" )
    	++j;
    
    if( j % 2 )	
    	throw (::std::string)"uneven number of coordinates in \"shape\"";
    
    i.seekg( old_position );    
             		 //<= hier ist i.tellg() == old_position.
    points.resize( j/2 );
    
    j = 0;          	
    while( read_word(i,word) && word != "end" )  //<= und hier wird gleich das 
                                                 //letzte element vor "end" eingelesen,
                                                 //obwohl da mehr sein sollten.
    {	                
    	string_to_double(word,points[j].x);
    	read_word(i,word);                  
    	string_to_double(word,points[j].y);        	
    	++j;
    	if( j >= points.size() )
    		throw (::std::string)"too many points in \"shape.points\".;
    }
    

    hoffe mal, dass "irgendwer eine antwort" hat... 🙂



  • Soviel ich weiss, geht das ned. Aber schau mal auf:
    www.cplusplus.com



  • Hallo,
    wenn du einmal bis zum Ende des Streams gelesen hast, dann ist das eof- und das fail-bit gesetzt. Du musst also vor weiteren Lesevorgängen erstmal istream::clear aufrufen.
    Sprich:

    i.clear();
    i.seekg( old_position );    
    ...
    


  • ok, ich lese die daten jetzt sofort ein. damit hat sich das problem erledigt.

    Green_Ghost schrieb:

    Soviel ich weiss, geht das ned. Aber schau mal auf:
    www.cplusplus.com

    da hatte ich nichts diesbezüglich gefunden...

    Hallo,
    wenn du einmal bis zum Ende des Streams gelesen hast, dann ist das eof- und das fail-bit gesetzt. Du musst also vor weiteren Lesevorgängen erstmal istream::clear aufrufen.

    aha. aber ich war doch noch gar nicht am ende 😃 . das mit dem clear könnte aber sein.

    danke @alle (beide).


Anmelden zum Antworten