In this video,
we'll learn about how c++ uses streams and files for IO.
Writing an IO library for any programming language is an extremely difficult task.
There can be all sorts of devices providing data to your program,
and your program can send data to many types of devices as well.
These devices may be physical devices such as hard disks,
the console and the keyboard or they can be virtual devices,
such as some connection to a web server.
C++ provides a stream abstraction to work with IO devices.
A stream is an interface that's independent of the actual device.
So from the programmer's perspective,
we can code to the stream interface,
and not worry too much about what device is connected to the stream.
A stream is exactly as you would expect. It's a sequence of bytes.
C++ provides different types of streams
depending on whether we want input or output,
but we can also use a stream that does both.
Let's see what this looks like visually.
In this slide, you can see that the c++ program is on the right side.
The upper stream is an input stream that provides
input to the program from some input device.
This device can be the keyboard of file, a connection to a web service and more.
The lower stream is an output stream that takes output from the program
and sends it to the output device.
Again, the output device can be a file, the console, a connection to a web service and more.
So let's see some of the c++ header files
that we typically include to allow us to work with iostreams.
In this slide, we can see three of the most commonly included header files
that allow us to work with stream IO.
There are others, but these three are the most commonly used.
First, we've already seen and used iostream.
Iostream provides the definitions required for formatted input and output
to and from streams.
The fstream include file
provides definitions for formatted IO to and from file streams.
And the iomanip header file provides definitions for manipulators
that allow us to format iostreams in specific ways.
We'll use the iomanip functionality first in this section.
When we include these header files,
we'll have access to many c++ classes that we can use for file IO,
so let's see a few of them.
The ios class provides basic support for formatted and unformatted io.
It serves as a base class for most other classes in the iostream hierarchy.
The ifstream class provides high-level input operations from files.
So if you want to read from a file, you can declare your object to be an ifstream object.
The ofstream class provides high-level output operations from files.
And again, if you want to create a new file or write to a file,
you can declare it as an object of ofstream.
The fstream class provides high-level IO on file-based streams.
Fstream is derived from ifstream and of osstream using multiple inheritance.
If we need to do both input and output to a file at the same time,
we can declare an fstream object to do it.
Finally, we have the stringstream class.
This is a very useful class that provides high level io
on memory-based strings.
Just like we can use insertion and extraction with cin and cout,
we can use them with strings to get input and provide output into a string in memory.
That's pretty powerful stuff, and we'll see some examples later on.
Finally, let's visit our old friends cin and cout
and see exactly what they are.
The fact that we've used them all along without worrying about how to create
them, how to connect them and so forth,
is a testament to the design of the c++ io library
and the device independence model.
Cin, cout, cerr, and clog
are global objects.
They're initialized before main begins to execute.
And all you need to do is include iostream to use them.
Cin is the standard input stream and is by default connected to the keyboard.
It's an instance of the istream class.
Cout is the standard output stream, and by default, it's connected to the console.
It's an instance of the ostream class.
This class should sound familiar since we used it when we overloaded the insertion operator
in the overloading operator section of the course.
Cin and cout are generally buffered streams.
This means that input from cin won't be automatic until the user presses enter.
And output to cout occurs only when the stream buffer fills up
or we provide a std endline or we flush the stream.
Now cerr and clog are the standard error stream
and the standard log stream, respectively.
By default, they're both connected to the console.
They're both unbuffered, which means that we get input or output from them
as we need it. Best practice is to use cerr for error messages
and clog for log messages.
Those of you who are familiar with the UNIX terminal shell or the windows command prompt.
Know that we can redirect these streams easily,
so input comes from a file or output goes to a file.
I won't be covering this in this course,
but if you're interested do an internet search for redirecting io,
and you'll see how easy and powerful it can be.
Okay. So that concludes this video. In the next video, we'll see how we can use the
IO manipulators to help us format our IO.