WEBVTT

00:00.480 --> 00:05.710
And let's finish off the rest of this mini project we're doing.

00:06.240 --> 00:13.710
So when you left off we still need to implement the precise context to me and we still have to have

00:13.790 --> 00:19.340
the same contacts so let's do that.

00:19.890 --> 00:25.660
So let's start with the recap resize contacts.

00:26.410 --> 00:28.080
This copy this

00:31.160 --> 00:32.910
Clairette up here.

00:38.290 --> 00:44.760
It's a it's not going to return anything.

00:44.870 --> 00:47.380
Take this reference

00:53.090 --> 00:53.570
so

00:56.010 --> 01:09.160
in it's the new capacitor you're right it's either new capacity for the data it's in wow.

01:09.270 --> 01:12.600
While we're at it let's also declare this on

01:23.030 --> 01:25.330
this churn anything

01:29.260 --> 01:32.020
in this one doesn't need to change.

01:32.130 --> 01:32.980
It's.

01:36.210 --> 01:48.260
K so it's implement these two.

01:49.930 --> 01:53.690
Let's move this up actually.

01:55.430 --> 02:02.240
Here because this is going to do file I own this is also an HD file.

02:02.430 --> 02:02.890
OK.

02:02.970 --> 02:06.300
And we'll save all the files for last.

02:06.300 --> 02:09.510
So let's do the resize now.

02:12.510 --> 02:16.730
So how will resize work.

02:17.220 --> 02:27.710
Basically what we want to do is to essentially we're going to create a brand new array of this new capacity

02:28.460 --> 02:35.850
and we're going to copy all of our current contacts into that new array.

02:36.380 --> 02:44.990
And then we're going to delete the current array and then assign it assigned the contacts divvies array

02:45.110 --> 02:48.130
to be the new the new one that we made.

02:48.480 --> 02:48.940
OK.

02:49.010 --> 03:04.460
So that's going to be something like make you array of size new capacity.

03:04.710 --> 03:08.520
Then we're going to copy all existing

03:11.170 --> 03:18.450
contact it's into the new array right.

03:19.780 --> 03:24.890
And then we're going to delete the

03:27.790 --> 03:36.390
old Ray and then we're going to set up contacts

03:38.340 --> 03:39.250
Eby

03:42.030 --> 03:48.370
winter contacts to be the new Eric

03:53.980 --> 04:02.370
cause you can't just delete the old array right away because we need we still need all those contacts

04:02.390 --> 04:05.360
right now otherwise we lose them all.

04:05.840 --> 04:11.480
So that's why we need to kind of make a new one and then copy all the existing contacts into into that

04:11.480 --> 04:18.590
new one then delete the old one then set the contacts be the new array.

04:18.720 --> 04:19.440
OK.

04:19.820 --> 04:23.480
So let's start with this one is pretty easy.

04:25.090 --> 04:26.520
Contact us

04:29.390 --> 04:32.630
or new contacts.

04:32.640 --> 04:35.470
Winter you

04:38.440 --> 04:44.860
capacity.

04:45.120 --> 04:56.760
Contact just make sure it's in then we need to copy all the existing contacts into this new array.

04:56.780 --> 04:57.030
Right.

04:57.030 --> 05:00.180
Because right now this is empty.

05:00.490 --> 05:08.000
Four times zero is this thing thin contacts divi DOT

05:11.820 --> 05:15.260
number.

05:17.960 --> 05:18.590
Plus

05:21.360 --> 05:21.860
k.

05:21.960 --> 05:28.860
So now we shared essentially just copy it up.

05:28.910 --> 05:31.200
So ster Oppy

05:36.470 --> 05:37.100
copy

05:40.230 --> 05:42.260
of the new contacts are

05:46.830 --> 05:50.000
dot watch first name

06:02.440 --> 06:05.710
that you've seen for the last name

06:14.770 --> 06:22.720
in the new scene for phone number

06:34.390 --> 06:37.940
in it for that.

06:38.070 --> 06:39.870
And then we will.

06:39.870 --> 06:43.710
First we should check to see if it's null pointer because if it's null pointer we don't want to delete

06:43.710 --> 06:44.660
it.

06:44.730 --> 06:47.480
So on tax.

06:50.300 --> 06:51.890
Mixtures not known pointer

06:54.580 --> 06:56.370
in if it's not in real

07:03.610 --> 07:06.060
back in will just set it.

07:06.070 --> 07:09.160
We know set it after all so your

07:23.110 --> 07:23.550
so

07:26.590 --> 07:38.740
and then we will set the pointer contacts the 0.03 should be the new contacts.

07:41.350 --> 07:44.670
Like that and then will also set the new

07:47.310 --> 07:47.880
capacity

07:56.850 --> 07:58.360
like that.

07:59.710 --> 08:03.080
So hopefully this makes sense of what's happening.

08:04.200 --> 08:11.740
So that's really it for resizing.

08:11.800 --> 08:16.300
Now we just need to load the contacts and save the contacts.

08:16.300 --> 08:16.960
OK.

08:17.530 --> 08:23.360
And let's do this by using binary files.

08:23.370 --> 08:27.280
Ok just to make it easy.

08:28.290 --> 08:31.910
So let's start with saving the contacts.

08:32.020 --> 08:36.340
So say oh lifestream file

08:39.510 --> 08:42.110
OK and we'll say out file

08:45.540 --> 08:50.700
open you see.

08:51.020 --> 08:52.840
Well I need the file name.

08:52.850 --> 08:54.290
So it's actually

08:57.030 --> 08:58.650
up here.

08:59.350 --> 09:02.850
So it's still

09:08.350 --> 09:19.680
and what his name it contacts or something been for binary by the way or it doesn't.

09:19.720 --> 09:23.000
That's not a real extension so don't worry about it.

09:25.470 --> 09:33.010
So open contacts name and we're going to open it for a binder

09:38.560 --> 09:42.280
OK and we'll check to see if we opened it.

09:49.950 --> 09:54.480
And if it is then we'll write it to the

09:57.500 --> 09:58.670
rate for that.

09:58.680 --> 10:08.450
And in this case again we still have to write it in in characters essentially.

10:08.490 --> 10:10.530
So one so this is one byte.

10:10.590 --> 10:13.700
So we have to write it in one byte chunks.

10:14.340 --> 10:19.750
So we'll just cast the contract.

10:20.350 --> 10:33.320
You need a pointer contacts to be source essentially setting it to be the first bite of this array.

10:33.870 --> 10:41.280
And then we need to know how many we're writing so how many writing is context to beat the number of

10:41.280 --> 10:45.140
contacts times the size of a contact

10:49.990 --> 10:57.960
so we're reading this many contacts and it's of this so it's so.

10:58.140 --> 11:05.390
And then we'll just close the file.

11:05.780 --> 11:08.970
And that's really it for the save contacts.

11:09.380 --> 11:19.640
And we only have one more to go which is the load contex So

11:22.070 --> 11:24.510
here we need the live stream

11:28.270 --> 11:28.880
final

11:31.280 --> 11:41.570
infile the open rate we're opening for reading or reading only just like we're opening this one for

11:41.570 --> 11:43.180
writing only.

11:45.860 --> 11:46.280
It's

11:52.040 --> 11:56.860
it's for binary.

11:57.340 --> 11:59.520
Would check to see if it's open

12:05.540 --> 12:14.080
and if it is OK we're going to use that same trick we did before you know how big the file.

12:14.090 --> 12:22.460
So if you remember we said in file C and C to the end right.

12:26.060 --> 12:28.970
And we need the like of

12:31.780 --> 12:41.910
file in bytes so infile that tell G Very.

12:41.990 --> 12:44.390
And then we go back to the beginning.

12:44.420 --> 12:44.720
Right.

12:44.730 --> 12:49.890
So in file this sheet

12:58.140 --> 13:06.060
so says go to the end of the file tell me what position the current byte is at which basically just

13:06.060 --> 13:13.500
gives me or current character is that and this just gives me how many bytes are in the file.

13:13.500 --> 13:13.820
Right.

13:13.920 --> 13:20.940
So if it's like 100 it's 100 bytes and then we just go back to the beginning so we can actually do the

13:20.940 --> 13:22.800
proper reading.

13:22.800 --> 13:26.930
So this only gives us how many bytes are in the file.

13:26.940 --> 13:30.920
But this doesn't tell tell us how many contacts we have.

13:30.920 --> 13:31.640
Right.

13:31.650 --> 13:40.610
So if we want to know how many contacts we have we have to say this number of contacts equals the length

13:41.450 --> 13:49.290
of file in bytes over the size of a contact or eight.

13:49.420 --> 13:53.220
So a contact has a certain size right in bytes.

13:53.420 --> 14:00.650
And if we'd divide the length of the file and bytes over that size that will tell us how many contacts

14:00.740 --> 14:04.160
we created initially.

14:06.720 --> 14:10.950
And then here will create the capacity

14:14.050 --> 14:18.970
as being the number of contacts

14:22.180 --> 14:25.900
times two plus 10.

14:26.120 --> 14:32.230
So we just kind of said how many there are based off of how many we wrote before.

14:32.410 --> 14:37.710
And then we just need to set all our contacts.

14:38.170 --> 14:48.000
Well first we need to actually set set pointer or actually knew the memory right.

14:48.270 --> 14:54.410
What do your contacts because when we load it when we only load it once at the very beginning.

14:54.720 --> 14:57.720
So when we load it this is set to no pointer.

14:57.750 --> 15:02.520
So it's not actually pointing at any memory at need point to that a memory

15:06.370 --> 15:07.850
city.

15:08.290 --> 15:17.610
This and then we say contacts he'd got a number of contacts we know how many that is

15:20.990 --> 15:23.410
in a capacity

15:31.330 --> 15:35.690
capacity like that.

15:36.070 --> 15:44.850
Now our context is it finally initialized now need to actually read it or read it.

15:44.890 --> 15:45.860
Sorry.

15:46.160 --> 15:51.350
Read should look exactly like the right for

16:03.270 --> 16:06.510
and we read how many bytes are in the fight right.

16:15.430 --> 16:17.330
Then just close the file

16:21.780 --> 16:25.880
and return true that it actually read everything.

16:29.060 --> 16:33.140
So that's it for that.

16:36.150 --> 16:37.490
So are we done.

16:40.060 --> 16:43.370
Oh we load the contacts right.

16:44.330 --> 16:46.820
And all that.

16:46.850 --> 16:49.430
So that's OK.

16:49.520 --> 16:53.420
And the last thing we should do is say if

16:56.430 --> 17:02.430
if we have loaded the contact or if we haven't learned the contacts we should kind of output that we

17:02.430 --> 17:02.790
didn't

17:05.660 --> 17:07.230
didn't.

17:07.700 --> 17:11.430
Error eating file

17:16.710 --> 17:18.330
the file name

17:24.870 --> 17:25.590
it's

17:28.270 --> 17:36.250
Annibale kind of return zero here because if we can't write read the file then we can't do anything

17:36.370 --> 17:37.400
right.

17:37.450 --> 17:40.190
So let's run this.

17:40.280 --> 17:40.770
You.

17:41.510 --> 17:49.230
It's so everything foul contacts up in.

17:49.350 --> 17:50.150
OK.

17:50.380 --> 17:53.840
So why why do you think that is.

17:53.990 --> 17:54.830
Right.

17:54.950 --> 18:00.070
So the reason is because the file doesn't exist.

18:00.370 --> 18:07.390
And we said before that if you actually write to the file it'll create the file but we'd never have

18:07.390 --> 18:08.520
written to the file.

18:08.530 --> 18:12.610
So how do we create it.

18:13.600 --> 18:16.330
Here's a little trick that we can do.

18:16.360 --> 18:18.610
So I'm going to write one more function

18:21.320 --> 18:33.430
in void and say create contact us file and copy this

18:36.970 --> 18:41.880
and we're going to paste it here.

18:46.540 --> 19:00.970
And we're going to try to see if we can open the file OK and if we can't open the file we will actually

19:01.000 --> 19:02.500
create the file.

19:04.510 --> 19:18.050
And open tax file name stream Harry.

19:18.120 --> 19:28.060
If not you can file is open so we couldn't open the file.

19:28.080 --> 19:30.260
That probably means we haven't actually created it.

19:30.300 --> 19:31.970
So we'll say off stream

19:36.880 --> 19:37.760
file.

19:39.780 --> 19:43.290
And we'll say oh file it open

19:45.860 --> 19:46.780
again.

19:49.090 --> 19:49.670
Street

19:52.100 --> 20:05.670
Nery write and I would like to effectively make an empty five day and how you do that is this is out

20:05.720 --> 20:12.430
file and insertion operator and we'll say flush and then close it

20:16.630 --> 20:18.490
so flesh will actually

20:21.520 --> 20:28.210
essentially write out nothing but this will still create the file.

20:28.860 --> 20:36.650
So that's kind of a nice little trick you can do if you just want to flush the entire file.

20:36.650 --> 20:41.970
So it basically just makes a 0 byte file is this.

20:42.300 --> 20:53.310
So it opens it or does kind of flush meaning a just please just create it right and then close and then

20:53.340 --> 20:57.380
otherwise otherwise you could open it.

20:57.380 --> 20:58.570
So you should close it

21:03.330 --> 21:07.730
and up here we will actually create its

21:12.120 --> 21:15.020
if it's not created already.

21:15.630 --> 21:20.410
And there's one thing that we should do after the loop is done.

21:25.150 --> 21:27.700
Which is save the contacts

21:30.100 --> 21:32.200
so if we quit we should just save them all

21:35.520 --> 21:41.670
and we should also delete the memory right.

21:41.730 --> 21:42.420
We haven't done that

22:09.170 --> 22:09.640
today.

22:09.650 --> 22:11.680
This is just good practice to do.

22:13.020 --> 22:18.140
So no I think that's kind of it.

22:18.230 --> 22:21.730
It's be missed anything right.

22:24.160 --> 22:27.510
So let's read this and see what happens.

22:30.360 --> 22:34.320
So notice we actually got to the main screen.

22:34.590 --> 22:39.210
So it actually was able to load even though it probably has nothing.

22:39.360 --> 22:40.230
OK.

22:40.230 --> 22:44.580
So what happens if we display nothing happens.

22:44.600 --> 22:48.200
Let's try to add something.

22:48.280 --> 23:01.150
So please enter the first name Fred last name and and please enter a number

23:03.750 --> 23:06.840
saved and see if that actually worked.

23:08.300 --> 23:14.540
So Fred Flintstone then has the very first phone number in existence right which makes sense.

23:15.120 --> 23:17.070
OK let's add something else

23:27.460 --> 23:28.190
OK.

23:28.270 --> 23:29.850
So there you go.

23:30.950 --> 23:38.990
So we have a list of contacts that we can add to which

23:44.390 --> 23:48.180
so flee all of that made sense.

23:48.170 --> 23:54.290
And we found this kind of interesting this is really just a good review.

23:54.380 --> 24:05.660
And it is also to illustrate to I guess more of the advanced students how to not always have to do a

24:05.660 --> 24:06.900
top down design.

24:06.980 --> 24:07.210
Right.

24:07.200 --> 24:11.120
We can kind of do this piecemeal as it makes sense.

24:11.130 --> 24:11.910
OK.

24:12.290 --> 24:20.830
Which is which is usually the more common method that programmers will use sort of.

24:21.050 --> 24:28.160
Right the usage code even though you don't have the code at all and then make sure that makes sense

24:28.220 --> 24:37.730
and it solves the problem and then actually go back and implement everything that that you had you declared

24:37.730 --> 24:39.050
before.

24:40.670 --> 24:46.700
So that's kind of a more natural way and that's usually the more it's usually the way I approach problems.

24:46.700 --> 24:56.510
Now I rarely do a top down design especially for really big projects you usually want to break a small

24:56.510 --> 25:03.760
piece of it right down and just implement that and then test it and see if it's working.

25:03.950 --> 25:05.860
And then kind of add on to it.

25:06.100 --> 25:13.650
And and that's really b approach will take for the very final project in this course.

25:13.880 --> 25:16.740
So I will see you in the next section.
