Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Executing
Program
Programming Defensively
The success or failure of a call to open a
file should always be tested
Use inStream.open()
Us in an assert( ) mechanism
Call before proceeding with additional
operations on the file
The Input Operator
We have used cin >> x;
Value entered via the keyboard
C++ uses the same operator to bring
values into variables from a stream
inStream >> reading;
The reading pointer keeps track of where
in the stream the program is currently
reading
The getline() Function
Requires an istream object, a string object
getline (nameStream, name);
Note: the>>
Note: the >>operator
operatordoes
doesnot not
Reads entire name into variableread
readthe
thenewline.
newline. The next>>
Thenext >>
skips
skipsititas
aswhite
whitespace.
space. But
Butififaa
Reads until it hits a newlinegetline
character usednext,
getlineisisused next,ititsees
sees
Newline character read, not the thenewline
added to and
newline andterminates.
variable
terminates.
Think
Thinkabout
aboutwhat
whathappens
happensififyou
you
mix>>
mix andgetline
>>and getlinecalls.
calls.
The eof() Message
Can be used as a sentinel value to control an input
loop
for ( ; ; ) {
inStream >> reading;
if (inStream.eof() ) break;
// . . . process the input }