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 33,285 of 33,346   
   guinness.tony@googlemail.com to nvangogh   
   Re: C++ Input Output problem   
   18 Feb 14 05:41:09   
   
   On Tuesday, 18 February 2014 05:33:33 UTC, nvangogh  wrote:   
   > I have come to a question in C++ Primer (p 314 , exercise 8.1) that is   
   > not clear to me.   
   >   
   > "Write a function that takes and returns an istream&. The function   
   > should read the stream until it hits end-of-file. The function should   
   > print what it reads to the standard output. Reset the stream so that it   
   > is valid before returning the stream."   
   >   
   > Breaking this down, the function has to do three things:   
   >   
   > 1. Read a stream until it hits end-of-file   
   > So the >> operator reads input from an istream object - cin.   
      
   operator>>() is not the only way of reading input from a stream.   
      
   > This stream's end of file can be interrogated by   
   > cin.eof(). This returns true if the end of file bit is set which can be   
   > tested with a bool variable   
   > bool on = false;   
   > on = cin.eof();   
   > if(on == true)   
   > // end of file is reached, else   
   > if(on ==false)   
   > // keep reading cin   
      
   Don't test bools against boolean constants - use them as actual booleans:   
      
   if (on)   
   {   
       // end of file is reached   
   }   
   else   
   {   
       // keep reading cin   
   }   
      
   > I don't believe that this is completely correct so can someone show me   
   > how this code should be presented?   
      
   Your suspicions are correct.  EOF status is only set *after* a read   
   operation.  It cannot be tested beforehand.  The canonical read-loop   
   simply stops when an input operation fails.  Testing eof() *afterwards*   
   is the means to distinguish between a "clean" EOF and an input-error   
   of some sort.   
      
   > 2. Print what is read to the standard output   
   > I can only imagine this to be cout << ? But am lost from here   
      
   Yes, std::cout/std::wcout and stdout are all channels for sending   
   data to "the standard output".   
      
   > 3. Reset the stream so it is valid before returning the stream   
   > This section of the problem again defeats me.   
      
   The "reset the stream" is, indeed, puzzling (it can be done, but   
   the underlying stream itself is probably still unusable thereafter).   
      
   > Can anyone help with this function?   
      
   Given the remit, I came up with this (which is almost certainly *not*   
   what the exercise is leading you to write - I don't know the context   
   of the tutorial in which it appears - you are probably expected to   
   write a loop in place of the first statement):   
      
   #include    
   #include    
   #include    
      
   std::istream& copy_to_standard_output(std::istream& source)   
   {   
       std::cout << source.rdbuf();   
       source.clear();   
       return source;   
   }   
      
      
   --   
         [ 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