Reputation: 35
Hi I am currently studying c++ from a beginners book. In the book the author gives a brief explanation of both header files istream
and ostream
. Unfortunately I don't quite understand what he means. I have tried to look them up online but it doesn't help me understand his explanation.
He says
istream
: Contains the extractors for inputting data from streams and includes the template classbasic_istream
. In other words,istream
puts the I in I/O.
ostream
: Contains the inserters for outputting a series of bytes and includes the templatebasic_istream
. basicallyostream
puts the O in I/O.
What I don't understand is why do you need extractors for inputting data from streams and vice-versa for the ostream
.
Upvotes: 1
Views: 1769
Reputation: 3379
What I don't understand is why do you need extractors for inputting data from streams and vice-versa for the
ostream
.
Say you have istream_obj
as input data stream. Now you write:
int x;
istream_obj>>x;
Here istream
will try to extract an int from istream_obj
with the overload like:
void operator >> (istream _stream, int x); // may not be exactly this but similar thing will be done.
This is what extraction from an istream means
Upvotes: 0
Reputation: 385274
Data that serves as input to your program must be extracted from the istream
that provides it.
Likewise, data that serves as output from your program must be inserted into the ostream
that spirits it away.
+------------------+ +-----------------------------------------+
| DATA SOURCE | ----input----> | [istream] --extractor--> YOUR PROGRAM |
+------------------+ +-----------------------------------------+
+------------------+ +-----------------------------------------+
| DATA SINK | <---output---- | [ostream] <--inserter--- YOUR PROGRAM |
+------------------+ +-----------------------------------------+
Upvotes: 3