WEBVTT

00:00.450 --> 00:03.650
Now let's look at how to read the input.

00:03.720 --> 00:09.320
Back in ands it's going to be a little bit more involved.

00:09.350 --> 00:23.330
So for this we're going to use the I am stream object of stringy ice cream type file

00:26.090 --> 00:29.330
in just like before we left you open the file

00:35.410 --> 00:38.870
read back in test text that we made last time

00:42.300 --> 00:47.680
and here we're going to check to see if we could open the file.

00:47.760 --> 00:56.860
So how you do as you say is open like this because what if maybe the file doesn't exist.

00:56.940 --> 00:58.230
So you couldn't open it.

00:58.280 --> 00:59.550
In that case.

00:59.760 --> 01:02.870
So we always have to check to see if our files open.

01:02.880 --> 01:07.050
We should be doing this with her out files as well.

01:08.720 --> 01:21.810
So here now what we want to do is actually figure out how big the file is and and you can really think

01:21.810 --> 01:37.560
of files as one big long array and that array will have a cursor where to where it currently points

01:37.560 --> 01:37.890
to.

01:37.890 --> 01:48.220
So you kind of think of it as one big one big array of bytes and the cursor really points to the current

01:48.220 --> 01:49.170
byte.

01:49.390 --> 01:55.870
So when you open the file the cursor is at the very start of the file.

01:56.110 --> 02:03.600
So we want to essentially move the cursor to the end of the file.

02:03.910 --> 02:08.610
Ok so how you do that as a Sikh.

02:09.080 --> 02:16.730
JE zero in five dot and.

02:16.980 --> 02:21.150
So this line will actually move the cursor of the file

02:25.150 --> 02:27.470
to the end of the

02:32.180 --> 02:35.220
Am i asking why you want to do that.

02:35.280 --> 02:35.970
OK.

02:36.240 --> 02:37.950
And this is the reason.

02:38.070 --> 02:42.900
So here now we can actually get how big the file is.

02:42.900 --> 02:51.130
In general I saying infile dot TLG.

02:51.210 --> 02:59.040
So this is actually what TLG does will return the position of the current character in The File.

02:59.100 --> 03:07.560
And since we know that it's at the end this will essentially give us how big the file is and bytes because

03:08.520 --> 03:11.940
it'll start at the first bite at the start.

03:11.940 --> 03:15.710
When you open the file and then you'll seek to the end of the file.

03:15.870 --> 03:24.750
Ok so the cursor is at the very end and TLG will actually tell you what what position the current character

03:24.840 --> 03:26.520
is in the file.

03:26.700 --> 03:28.590
So each character is one byte.

03:28.680 --> 03:32.630
So this will actually give you the entire length of the file.

03:34.250 --> 03:39.250
So now we want to actually read from it again.

03:39.290 --> 03:50.050
So we should actually put the cursor back to the start that by saying zero in filed the beginning.

03:51.320 --> 04:12.270
This will move the cursor you the starch and this will again give the current character or the position

04:12.270 --> 04:21.800
of the current character.

04:21.830 --> 04:26.170
So hopefully this makes sense what's happening here.

04:26.210 --> 04:34.490
So we're moving the cursor of the file every file has a cursor to the very end of the file.

04:34.800 --> 04:35.410
OK.

04:35.660 --> 04:41.870
And then we're getting the current position of the end of the file which really just gives you how many

04:41.870 --> 04:44.010
bytes there are in the file.

04:44.060 --> 04:45.460
This is what this really does.

04:45.480 --> 04:46.620
Second line.

04:46.970 --> 04:52.050
And then we're just moving the cursor back to the start of the file.

04:52.880 --> 04:59.780
And we did all that because we needed the length of the file and we need the length of the file because

04:59.780 --> 05:07.760
we need to know how much to store in our array.

05:08.370 --> 05:20.230
And we're going to make it dynamically and B link plus one can and then using the infile again.

05:20.530 --> 05:29.440
We'll use the get method and actually use this get method for in hangman if you remember to get an entire

05:29.440 --> 05:30.840
C string.

05:31.030 --> 05:33.030
So that's essentially what we're doing here.

05:34.770 --> 05:38.540
We're just getting this E-string of this link.

05:40.550 --> 05:46.380
So this is actually put all all the contents of the file into our new buffer that we created.

05:46.700 --> 05:50.190
So this is the power of dynamic memory.

05:50.360 --> 05:55.350
We have no idea how big the file is in the first place but we can figure it out.

05:55.370 --> 06:03.530
And then just create a buffer to or an array to store it all day.

06:03.740 --> 06:12.080
And then once we get it we'll just put it and this time we actually will use out.

06:12.190 --> 06:17.530
And if you remember that actually we'll put the null character in at the end.

06:17.790 --> 06:21.100
And that's why we have length plus one here

06:23.810 --> 06:37.060
and I'll put it in and we'll actually just delete our buffer array and we'll close the file here.

06:44.100 --> 06:55.140
So if we run this hopefully this will actually output what we input into the file last time we run it

06:55.710 --> 06:56.820
like this.

06:57.040 --> 06:58.100
Hello file.

06:58.180 --> 07:01.830
So.

07:01.860 --> 07:07.550
So hopefully all this made sense and that's a little bit more complicated.

07:07.830 --> 07:14.550
But really this just gives you the length of file or how many bytes the file is.

07:14.850 --> 07:17.190
There should be no surprise to you right.

07:17.280 --> 07:23.710
We're just creating a buffer to put all the characters of the file into.

07:23.760 --> 07:28.410
So this will store all the characters of the buffer and we just read it.

07:28.590 --> 07:31.170
And this is what this getline does.

07:31.170 --> 07:32.220
And we just read

07:35.540 --> 07:40.680
read all the characters in the file and then put all those characters into this buffer.

07:40.960 --> 07:41.750
OK.

07:41.910 --> 07:45.000
And then we just put the buffer delete it and then close the file.

07:45.110 --> 07:49.210
So that's really it for reading.

07:49.410 --> 07:51.060
OK.

07:51.220 --> 08:00.200
And we'll be going over more examples of reading texts in in future lectures.

08:00.790 --> 08:05.290
But in the next lecture I want to go over binary files.
