WEBVTT

00:00.930 --> 00:05.790
We don't have to use just textfiles a lot of the time.

00:05.790 --> 00:14.710
All we want is to store some data in REPAK quickly not caring about the readability of the file itself.

00:15.420 --> 00:21.540
And binary files are the perfect complement to this.

00:21.540 --> 00:27.100
So what I've done here a start is I've created a structure.

00:27.120 --> 00:39.120
It has two members one in value two and it's integers and I've created a an array of this type and type

00:39.120 --> 00:47.370
data and there is just about 10 of them here Max is 10 and it is randomly stores and values into it.

00:47.450 --> 00:47.960
OK.

00:48.060 --> 00:50.080
And I'm just outputting the results.

00:50.230 --> 00:58.540
So you run this just randomly stores a bunch of Vout.

00:58.650 --> 00:59.660
OK.

01:00.140 --> 01:10.740
And I'm doing this because we're going to actually write out these values into a binary file and then

01:10.800 --> 01:14.330
read them back into a different array.

01:18.050 --> 01:24.560
So let's get started of how to write something right.

01:24.560 --> 01:26.050
File a binary file

01:28.830 --> 01:29.400
stream

01:32.740 --> 01:34.210
a file

01:36.770 --> 01:39.230
and when we opened the file

01:45.770 --> 01:48.210
it dated been this doesn't exist yet.

01:48.320 --> 01:49.830
Will.

01:50.080 --> 01:54.870
And when we open it will actually say it's a binary file.

01:56.000 --> 01:57.030
Like this.

01:57.140 --> 02:01.120
Use extreme and colon colon binary.

02:01.580 --> 02:02.220
OK.

02:02.480 --> 02:13.160
And so now that we have opened it in the binary kind of format We'll write to it and we can't just use

02:13.160 --> 02:16.850
the insertion operator like we do with Seow tour.

02:16.850 --> 02:23.600
What we did before we actually have to call a different function OK because the insertion operator really

02:23.600 --> 02:30.070
only works with Tex or possibly like integers just like just basic types like that.

02:30.190 --> 02:36.280
OK so what we're going to do is call out file dot right.

02:38.780 --> 02:50.210
And right actually we'll write code bytes essentially and we know that a character is one byte.

02:50.240 --> 02:59.810
So we're going to do is actually cast this to be the character character pointer and we're going to

02:59.810 --> 03:01.910
take the address of the array data

03:06.140 --> 03:08.210
and right to take.

03:08.210 --> 03:13.870
So this is the data that we're going to write to the file and write needs.

03:13.880 --> 03:18.710
The length of how much how many bytes to write.

03:19.220 --> 03:23.930
And to do that you use the size of orated

03:27.670 --> 03:28.470
k.

03:28.730 --> 03:32.280
So this is kind of interesting.

03:32.770 --> 03:41.330
If you take the size of the array and this array that's kind of static.

03:41.570 --> 03:43.820
Well it's made on the stack.

03:43.940 --> 03:44.470
OK.

03:44.510 --> 03:45.040
Right.

03:45.080 --> 03:47.410
This isn't dynamics that's made on the stack.

03:47.690 --> 03:54.530
So if you actually take a size of an array as create on the stack it'll actually give you how many bytes

03:54.530 --> 03:58.460
it is and total including each element.

03:59.940 --> 04:04.150
This wouldn't work if you used a dynamic array.

04:04.380 --> 04:08.520
If you use a dynamic array it'll just be the size of the pointer.

04:08.740 --> 04:21.840
OK so in this one his first parameter we're taking the address of the of the array.

04:22.280 --> 04:25.780
And then we're it to be this character pointer.

04:25.780 --> 04:31.850
So this looks a little bit complicated but really all it's saying is be that the first element and B

04:33.140 --> 04:36.660
type character pointer.

04:37.020 --> 04:38.730
That's that's really all this is saying.

04:38.730 --> 04:46.080
So it's really just it's kind of making a cursor and it's pointing at the very first bite of this radiate.

04:46.210 --> 04:48.490
So that's really all this is saying.

04:48.980 --> 04:54.020
Because cause right only works with bytes right.

04:54.320 --> 04:58.400
Each byte or this many bytes.

05:00.110 --> 05:12.850
And so now we will just close it for and what we're going to do is actually read it back in right now.

05:13.240 --> 05:14.990
But in a new array.

05:15.140 --> 05:19.470
So a new array of Max.

05:19.600 --> 05:23.560
So we're not going to use the same array data used for.

05:23.970 --> 05:26.930
And we're going to make a infile.

05:26.950 --> 05:47.070
So I have stream in file in file open loop in the same file open for binary reading.

05:47.130 --> 05:48.740
It's a binary reading.

05:48.790 --> 05:54.000
The last time and essentially we're going to use the same kind of signature.

05:54.010 --> 06:02.170
But this is this time it's not going you're right as can be read in file dot read

06:05.310 --> 06:07.600
you begin the same thing.

06:15.190 --> 06:22.850
Like this looks exactly the same as right but now is using right.

06:24.260 --> 06:25.910
And we just close it

06:31.520 --> 06:36.770
and now we're going to just loop through the new array and just output what we got.

06:36.780 --> 06:40.620
So hopefully they'll match up.

06:41.120 --> 06:44.790
Max plus

06:47.770 --> 06:51.420
put everything re

07:37.000 --> 07:37.790
k.

07:38.130 --> 07:41.410
So it's basically it I think.

07:41.410 --> 07:50.950
So again we're just filling the array data with some random values and then rewriting it in a binary

07:50.950 --> 07:51.520
way.

07:51.550 --> 07:56.930
And those that we're using it doesn't matter that these are structures.

07:57.360 --> 08:04.890
As long as it knows how big you can make or how many bytes it is.

08:05.310 --> 08:14.850
So it knows it because it's made on the stack so it knows that the size of this data is what 8 bytes.

08:14.850 --> 08:18.190
And there's 10 of them so it's 80 bytes right.

08:18.200 --> 08:19.350
Right right.

08:19.470 --> 08:24.560
So size of can actually do that calculation kind of Two-Step calculation.

08:24.820 --> 08:25.720
OK.

08:26.100 --> 08:36.470
So writing 80 bytes from this array to this fight then we're reading 80 bytes again into this new array.

08:36.510 --> 08:41.490
So you run this you will see.

08:41.820 --> 08:44.450
So it is.

08:44.500 --> 08:52.900
So this first one is first kind of 10 values here are all of this.

08:52.940 --> 08:59.460
So 29 to do 10 84 125 of 28 center etc..

08:59.860 --> 09:02.590
And then now we're reading it back.

09:02.680 --> 09:07.760
So 9:31 for 125.

09:08.200 --> 09:09.150
Cetera.

09:09.220 --> 09:10.710
So it's working.

09:10.780 --> 09:20.990
So this really binary files are really good for making kind of smaller files on the disk.

09:21.260 --> 09:27.570
And it's also really faster to write and read it usually.

09:27.730 --> 09:35.800
And that's usually why we want to use binary files over text files because they're really fast to write

09:35.800 --> 09:41.800
out and they take up very little space because it's all kind of nicely packed together.

09:41.800 --> 09:49.210
And usually it's actually quite a bit easier to read in as well and really use three lines to write

09:49.210 --> 09:50.570
it out in three lines.

09:50.570 --> 09:52.640
Are you back in.

09:52.960 --> 09:58.540
So as long as you can get past what's happening here in this line.

09:58.780 --> 10:05.280
It's pretty simple to write out a binary file and then read it back and.

10:05.730 --> 10:11.810
And we'll be using them in kind of a mini project in the next lecture.
