home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,711 of 33,346   
   guinness.tony@googlemail.com to mario...@googlemail.com   
   Re: Help reading a file and picking out    
   07 Dec 12 12:48:13   
   
   On Thursday, December 6, 2012 5:16:30 PM UTC, mario...@googlemail.com wrote:   
   > Hello everyone,   
      
   (snip)   
      
   > I'm kinda new to c++ so bear with me a little bit :P   
      
   (snip)   
      
   > myobject.close();   
   > myobject.open("Model_1.txt");   
      
   Over and above Frank's excellent comments, I'd like to point out that the   
   two lines I've cited above probably don't do what you think they do.   
      
   You've already been reading the file to exhaustion, so myobject's state   
   is now "fail" (that was the reason your first loop terminated) and "eof".   
      
   Neither closing nor re-opening a file on myobject will change that state   
   (open() will either leave an object's state as it found it or will add   
   "fail" and/or "bad" into its state).   
      
   Therefore it is guaranteed that your second loop will never run.  The   
   object is already at "eof" so the test fails immediately.  If you   
   switch to the correct loop construct (as Frank advised), you will   
   find that no input will be performed because myobject is still in   
   the "fail" state.   
      
   Before re-using the file object, you *must* reset its internal state   
   to that of an unused stream: do this by calling myobject.clear()   
   after closing the file and before re-opening it.   
      
   Regards,   
   Tony.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca