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,284 of 33,346   
   Barry Schwarz to nvangogh@pcexpert.net   
   Re: C++ Input Output problem   
   18 Feb 14 05:40:31   
   
   From: schwarzb@dqel.com   
      
   On Mon, 17 Feb 2014 21:33:33 -0800 (PST), 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.   
      
   Instead of cin which is the standard input, the >> operator should be   
   applied to the stream reference that is passed to the function.   
      
   There are other tools such as std::getline function and   
   std::istream::getline that can be used to read from the 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   
      
   There is no need for the variable.  A simple   
       if (stream_parameter.eof())   
           // handle end of file   
       else   
           // process data   
   achieves the same effect.   
      
   >I don't believe that this is completely correct so can someone show me   
   >how this code should be presented?   
      
   Tell us what you think is missing or incorrect.  There is no error   
   handling but the problem description doesn't ask for any.   
      
   >2. Print what is read to the standard output   
   >I can only imagine this to be cout << ? But am lost from here   
      
   You put the data somewhere when you read it.  That is where your <<   
   will find it.   
      
   >3. Reset the stream so it is valid before returning the stream   
   >This section of the problem again defeats me.   
      
   Take a look at the various ios member functions.   
      
   >Can anyone help with this function?   
      
   --   
   Remove del for email   
      
      
         [ 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