- [Instructor] The iostream libraries fairly rich
but most of it's there as back end support.
And there's really just a few classes
that you'll normally use.
This is iostream.formatting.cpp
from chapter nine of the exercise files.
You notice I'm including the iostream header
which is where most of the iostream stuff lives
The string header
'cause I'm going to be using some STL strings
and also iomanip
which is where the iostream manipulators are.
And we'll take a look at that in a minute.
You'll notice here this very common line
of code with c out and end l
and putting the string hello world out to the console.
What end l does is it puts a new line
and it also flushes the output buffer.
So I can get a new line if I wanted to
with just a backslash n like that,
but the advantage of end l is that
it also flushes the output buffer,
iostreams coming to basic flavors
the input streams and the output streams.
We're going to take a look at an input stream here.
We've been using output streams like that quite a bit.
The input stream here is cin
and it gets input from the console one word at a time.
And so I'm just going to put a return zero here
which will give me a warning down here
that this code will never be executed
and of course we know that
and I need to declare a string I s t r
for the input string and put out a prompt
and then grab just one word from the console and put it out.
And so when I build and run this
you notice I can type a bunch of words down here
and it'll just return that one word
and so that's how cin works.
It's just one word at a time.
And if I wanted to get more than one word at a time
I can comment this out
and I can use get line instead.
And that would look something like this.
I need to use a C string for this
so declare a character buffer of say 128 characters
and then standard cin.getline.
And here's my buffer goes here
and the size of my buffer goes here.
So I can just use size of like that.
And then I can see out the result.
And when I build and run this
I can say, this is a bunch of words
and it'll put out the whole string.
And so that's the two different ways
that you can use cin either one word at a time
or a whole line at a time.
So I'm going to just get rid of this
and I'll uncomment that
and then I'm going to comment out all of these
from the hello world
and get rid of our return zero
so that we can look at the rest of this.
This is output formatting,
which is more commonly done really with printf
because printf is a lot more convenient for this
and it's just a lot easier in a lot of ways.
But this is sometimes done,
the output formatting is done with manipulators
which are found in the iomanip header.
And here I've got some integers,
here we're going to do some integer formatting.
I'm just going to go ahead and build and run all of this
and we can look at it in sections.
Here's our integer section,
you see the default is to print them all out in decimal
and just like you would expect them.
You can also do them in hexadecimal
with the manipulator that says hex.
And so this is the way the manipulators work.
You just drop them into the stream
where you want them to have an effect
otherwise use them like you would any other object
in the stream.
And so hex prints things out in hex show base
which is this one here will show the base
so it's a zero x two a, instead of just two a.
You can of course do octal as well,
and here's our octal and with show base,
it puts a zero before them
which is traditional for octal numbers.
And then you can get back to default
by saying no show base and standard decimal.
And we can look at our floating point options here.
Here, we have some doubles
and you notice that the default is
to just print them out kind of like that
in whatever way seems convenient,
you notice that we're printing out six
significant digits here,
or if it's, you know far outside of those six digits
then it'll give us scientific notation
or we can declare what we want,
we can say fixed and you'll notice
that the very small number doesn't have
any digits within its range.
We can say scientific
and it'll print everything in scientific notation.
We can set the precision to three digits,
this sets it to three digits after the decimal point,
it looks like
or seven with the scientific notation
gives us seven significant digits
with the scientific notation or back to default.
We have string formatting options
which are very rudimentary.
Here's three strings
and we just put them out in default here
and you can see there's our strings,
or we can set the width to 64 characters
and set it to right flush.
We can set it to fill with dashes before
and we can just reset it back to left like this.
So that's the output formatting,
the other thing you can do
with iostreams is to use them with files.
And this is also fairly common.
So I'm going to go ahead and delete this and run clean
and bring up iostream-file.cpp,
which looks like this
here I have the iostream header
and the fstream header for working with files.
And what we're going to do here is very simple.
We're just going to write a file
and then read a file and then delete the file.
And so for writing the file
I've got an integer and a couple of characters strings
and ofstream opens a file for output.
And likewise ifstream opens a file for input.
And I give it the name of the object
'cause I'm actually creating an object with this
and the file name and the same with the ofstream.
So I got ofile and the file.
And then I can take that object in this case ofile
and I can use it same way I would
with something like cout
and just write the text out to the file.
And then I can say, ofile.close
and it closes the file.
And the same with the ifstream
the in file I can use getline
and I test whether we're to the end of the file
with infile.good.
And then I can just output my buffer to the console
and close the file.
And so we delete the file
with the standard library function remove
which we've seen before.
And I can go ahead and build and run this.
And you see, we write the file,
we read the file and we delete the file.
So the iostream library is very useful
for basic generalized I/O,
including interfacing with the console
writing to and reading from files
for more control you'll want to use
the C standard library functions
for reading and writing files.
And for more details on iostreams
please see the companion course
C ++ Templates and the STL.