WEBVTT

00:00.710 --> 00:06.790
Up until this point we've only ever dealt with the console for input and output.

00:07.760 --> 00:10.100
This doesn't have to be the case though.

00:10.340 --> 00:19.500
We could actually do input and output with files instead to do this will be using the F stream library.

00:19.550 --> 00:21.730
So here's how to use it.

00:21.860 --> 00:37.560
So just clewed F Street K stream of course stands for file stream and this is the standard C++ file

00:37.620 --> 00:39.860
input and output library.

00:40.740 --> 00:44.720
Stream is broken up into O.F. stream for output.

00:45.040 --> 00:49.140
And I have stream for input.

00:49.200 --> 00:57.660
You could also use it as just a stream for both input and output but for this course we'll be using.

00:57.900 --> 01:05.140
We'll be going to use O.F. stream and I have streams separately at least so you can get a feel for things.

01:05.730 --> 01:07.810
Let's see a simple example.

01:07.830 --> 01:11.430
Let's say I wanted to write to a file.

01:11.660 --> 01:16.400
So I'm going to be using O.F. string here.

01:16.500 --> 01:18.510
O f stream.

01:20.260 --> 01:20.780
File

01:24.310 --> 01:27.050
in file.

01:28.960 --> 01:30.330
He will open the file.

01:30.350 --> 01:40.130
So you always have to open a file that you're going to read from or write to.

01:40.940 --> 01:49.960
And in this case this open will actually create the file as long as you write something into it.

01:50.880 --> 01:56.170
So if test that TXI didn't exist it will actually create it.

01:57.320 --> 02:03.800
And we actually use the insertion operator just like we did with C out.

02:03.800 --> 02:16.080
In fact lifestream and IO stream are somewhat closely related so just say hello file.

02:16.090 --> 02:28.010
I know and we can even use an endl for new lines in the file and then once we're done we will close

02:28.010 --> 02:33.020
the file.

02:33.040 --> 02:42.490
So to recap we're making a variable of type O.F. stream live stream is not a structure like we've been

02:42.640 --> 02:44.610
using before.

02:44.680 --> 02:51.940
It's an object which we won't go into in this course just because there's so much to go over with objects

02:53.050 --> 03:02.930
but just know that stream is a type and it's actually again quite similar to see out in many ways the

03:02.930 --> 03:04.080
next line.

03:04.220 --> 03:07.830
We use our out file variable to open a file.

03:07.950 --> 03:08.450
OK.

03:08.510 --> 03:13.970
In this case test DXi DX T.

03:14.420 --> 03:18.680
If it doesn't exist it will be created for you but only if you write to it.

03:18.970 --> 03:21.030
And that's very important.

03:22.160 --> 03:27.400
The next line uses the familiar insertion operator to write to the file.

03:27.420 --> 03:29.940
And again we can even have andle.

03:30.070 --> 03:38.940
And so the last thing we do is close the file and we should always close the file once we're done.

03:39.410 --> 03:45.380
We're done writing to it or reading from it some strange and very bad things could happen if you're

03:45.380 --> 03:47.790
not if you're not doing that.

03:47.920 --> 03:48.860
OK.

03:49.190 --> 03:53.420
So let's run this and see what happens.

03:57.960 --> 04:01.670
So there's no output you'll notice OK.

04:02.100 --> 04:09.290
And we should try to check to see if this test RTX t was written somewhere.

04:09.310 --> 04:12.720
So we go to our directory.

04:12.720 --> 04:17.090
So this is the actual workspace directory for all our projects.

04:17.190 --> 04:19.300
I have a bunch here.

04:20.070 --> 04:26.100
All the ones we've done so far and in file I O which is the current project I'm working on.

04:26.730 --> 04:31.120
You'll see this test XTi.

04:31.470 --> 04:34.810
It's 14 bytes and it's plain text document.

04:35.040 --> 04:44.680
And even in the little preview window you can see it wrote the string that we output to it.

04:44.860 --> 04:47.160
And so you open this

04:51.030 --> 04:56.750
so you'll see the actual string up to it.

04:57.730 --> 05:08.600
So in the next lecture I want to go over how to read this back into our program.
