Beruflich Dokumente
Kultur Dokumente
CSCE 121:509-512
1
Based on slides created by
Stroustrup
SetBjarne
10: Input/Output
Streams and Jennifer Welch
Outline
Fundamental I/O concepts
Files
opening and closing
reading and writing streams
I/O errors
Reading a single integer
User-defined I/O
CSCE 121:509-512
Set 10: Input/Output Streams
device driver
input library
our
ourprogram
program
output library
device driver
CSCE 121:509-512
Set 10: Input/Output Streams
data
destination:
output device
somewhere
(1,234)
ostream
123
buffer
An ostream
turns values of various types into character
sequences
sends those characters somewhere (console,
file, main memory, another computer)
CSCE 121:509-512
Set 10: Input/Output Streams
somewhere
(1,234)
123
istream
buffer
An istream
turns character sequences into values of
various types
gets those characters from somewhere
(console, file,CSCE
main
memory, another
121:509-512
5
Set 10: Input/Output Streams
computer,)
extensible
define your own I/O operations for your own
types
a stream canCSCE
be121:509-512
attached to any I/O or
6
Set 10: Input/Output Streams
storage device
Files
Main memory is transient
when power goes off, contents of main
memory is lost
Files
General model
disk
I/O system
iostreams
Files
(sequences of bytes)
CSCE 121:509-512
Set 10: Input/Output Streams
main memory
Objects
(of various types)
Reading a File
1.
2.
3.
4.
Writing a File
1. Name the file
2. Open the file for writing
either an existing file or create a new
one
10
11
12
13
CSCE 121:509-512
Set 10: Input/Output Streams
14
15
CSCE 121:509-512
Set 10: Input/Output Streams
16
states:
good()//theoperationsucceeded
eof()//wehittheendoftheinput
fail()//somethingunexpectedhappened
bad()//somethingunexpectedandserioushappened
clear()
while(cin>>var)/*whileloopbody*/
if(!ist)/*reporterror*/
123
123
123
state
CSCE 121:509-512
Set 10: Input/Output Streams
19
CSCE 121:509-512
Set 10: Input/Output Streams
20
21
22
reading values
prompting user for input
writing error messages
skipping past bad input characters
testing the input against a range
23
Reading an Integer in a
Range
intget_int(intlow,inthigh,
conststring&greeting,
conststring&sorry)
{
cout<<greeting<<between<<low<<and
<<high<<endl;
while(true){
intn=get_int();
if(low<=n&&n<=high)returnn;//success
cout<<sorry<<endl;
}
}
CSCE 121:509-512
Set 10: Input/Output Streams
24
Reading an Integer
intget_int()
{
intn=0;
while(true){
if(cin>>n)returnn;//success
cout<<Notanumber,tryagain\n;
skip_to_int();
}
}
CSCE 121:509-512
Set 10: Input/Output Streams
25
voidskip_to_int()
//precondition:ciniseitherfail()oreof()
{
if(cin.fail(){//foundsomething,butnotanint
cin.clear();//wewanttolookatthecharacters
//whydoesthisforloopnotneedtoincrement?
for(charch;cin>>ch;){
if(isdigit(ch)||ch==){
cin.unget();//putdigitbacksowecanreadint
return;
//ifnotadigitorthenignoreitandcontinue
}
}
//onlyremainingoptioniseof()
error(noinput);
}
CSCE 121:509-512
Set 10: Input/Output Streams
26
27
User-Defined Output:
operator<<()
Must return a reference to an
ostream
First parameter must be a reference
to an ostream
Second parameter must be a
reference to an object of your type
ostream&operator<<(ostream&os,constDate&d)
{
returnos<<(<<d.year()<<,<<d.month()
<<,<<d.day()<<);
CSCE 121:509-512
28
}
Set 10: Input/Output Streams
CSCE 121:509-512
Set 10: Input/Output Streams
29
User-Defined Input:
operator>>()
Must return a reference to an istream
First parameter must be a reference
to an istream
Second parameter must be a
reference to an object of your type
CSCE 121:509-512
Set 10: Input/Output Streams
30
operator>>() Example
istream&operator>>(istream&is,Date&dd)
//Assumedateisinformat(year,month,day)
{
inty,d,m;
charch1,ch2,ch3,ch4;
is>>ch1>>y>>ch2>>m>>ch3>>d>>ch4;
if(!is)returnis;//wedidntgetourvalues;leave
if(ch1!=(||ch2!=,||ch3!=,
||ch4!=)){
//oops:formaterror
is.clear(ios_base::failbit);//setstreamstatetofail
returnis;//leave
}
dd=Date(y,Month(m),d);//updateddviacopyassignment
returnis;//leavewithisingoodstate
}
CSCE 121:509-512
Set 10: Input/Output Streams
31
CSCE 121:509-512
Set 10: Input/Output Streams
32
Acknowledgments
Slides are based on those for the
textbook:
http://www.stroustrup.com/Programming/10_iostr
eams.ppt
CSCE 121:509-512
Set 10: Input/Output Streams
33