Up until this point we've only ever dealt with the console for input and output.
This doesn't have to be the case though.
We could actually do input and output with files instead to do this will be using the F stream library.
So here's how to use it.
So just clewed F Street K stream of course stands for file stream and this is the standard C++ file
input and output library.
Stream is broken up into O.F. stream for output.
And I have stream for input.
You could also use it as just a stream for both input and output but for this course we'll be using.
We'll be going to use O.F. stream and I have streams separately at least so you can get a feel for things.
Let's see a simple example.
Let's say I wanted to write to a file.
So I'm going to be using O.F. string here.
O f stream.
File
in file.
He will open the file.
So you always have to open a file that you're going to read from or write to.
And in this case this open will actually create the file as long as you write something into it.
So if test that TXI didn't exist it will actually create it.
And we actually use the insertion operator just like we did with C out.
In fact lifestream and IO stream are somewhat closely related so just say hello file.
I know and we can even use an endl for new lines in the file and then once we're done we will close
the file.
So to recap we're making a variable of type O.F. stream live stream is not a structure like we've been
using before.
It's an object which we won't go into in this course just because there's so much to go over with objects
but just know that stream is a type and it's actually again quite similar to see out in many ways the
next line.
We use our out file variable to open a file.
OK.
In this case test DXi DX T.
If it doesn't exist it will be created for you but only if you write to it.
And that's very important.
The next line uses the familiar insertion operator to write to the file.
And again we can even have andle.
And so the last thing we do is close the file and we should always close the file once we're done.
We're done writing to it or reading from it some strange and very bad things could happen if you're
not if you're not doing that.
OK.
So let's run this and see what happens.
So there's no output you'll notice OK.
And we should try to check to see if this test RTX t was written somewhere.
So we go to our directory.
So this is the actual workspace directory for all our projects.
I have a bunch here.
All the ones we've done so far and in file I O which is the current project I'm working on.
You'll see this test XTi.
It's 14 bytes and it's plain text document.
And even in the little preview window you can see it wrote the string that we output to it.
And so you open this
so you'll see the actual string up to it.
So in the next lecture I want to go over how to read this back into our program.