And let's finish off the rest of this mini project we're doing.
So when you left off we still need to implement the precise context to me and we still have to have
the same contacts so let's do that.
So let's start with the recap resize contacts.
This copy this
Clairette up here.
It's a it's not going to return anything.
Take this reference
so
in it's the new capacitor you're right it's either new capacity for the data it's in wow.
While we're at it let's also declare this on
this churn anything
in this one doesn't need to change.
It's.
K so it's implement these two.
Let's move this up actually.
Here because this is going to do file I own this is also an HD file.
OK.
And we'll save all the files for last.
So let's do the resize now.
So how will resize work.
Basically what we want to do is to essentially we're going to create a brand new array of this new capacity
and we're going to copy all of our current contacts into that new array.
And then we're going to delete the current array and then assign it assigned the contacts divvies array
to be the new the new one that we made.
OK.
So that's going to be something like make you array of size new capacity.
Then we're going to copy all existing
contact it's into the new array right.
And then we're going to delete the
old Ray and then we're going to set up contacts
Eby
winter contacts to be the new Eric
cause you can't just delete the old array right away because we need we still need all those contacts
right now otherwise we lose them all.
So that's why we need to kind of make a new one and then copy all the existing contacts into into that
new one then delete the old one then set the contacts be the new array.
OK.
So let's start with this one is pretty easy.
Contact us
or new contacts.
Winter you
capacity.
Contact just make sure it's in then we need to copy all the existing contacts into this new array.
Right.
Because right now this is empty.
Four times zero is this thing thin contacts divi DOT
number.
Plus
k.
So now we shared essentially just copy it up.
So ster Oppy
copy
of the new contacts are
dot watch first name
that you've seen for the last name
in the new scene for phone number
in it for that.
And then we will.
First we should check to see if it's null pointer because if it's null pointer we don't want to delete
it.
So on tax.
Mixtures not known pointer
in if it's not in real
back in will just set it.
We know set it after all so your
so
and then we will set the pointer contacts the 0.03 should be the new contacts.
Like that and then will also set the new
capacity
like that.
So hopefully this makes sense of what's happening.
So that's really it for resizing.
Now we just need to load the contacts and save the contacts.
OK.
And let's do this by using binary files.
Ok just to make it easy.
So let's start with saving the contacts.
So say oh lifestream file
OK and we'll say out file
open you see.
Well I need the file name.
So it's actually
up here.
So it's still
and what his name it contacts or something been for binary by the way or it doesn't.
That's not a real extension so don't worry about it.
So open contacts name and we're going to open it for a binder
OK and we'll check to see if we opened it.
And if it is then we'll write it to the
rate for that.
And in this case again we still have to write it in in characters essentially.
So one so this is one byte.
So we have to write it in one byte chunks.
So we'll just cast the contract.
You need a pointer contacts to be source essentially setting it to be the first bite of this array.
And then we need to know how many we're writing so how many writing is context to beat the number of
contacts times the size of a contact
so we're reading this many contacts and it's of this so it's so.
And then we'll just close the file.
And that's really it for the save contacts.
And we only have one more to go which is the load contex So
here we need the live stream
final
infile the open rate we're opening for reading or reading only just like we're opening this one for
writing only.
It's
it's for binary.
Would check to see if it's open
and if it is OK we're going to use that same trick we did before you know how big the file.
So if you remember we said in file C and C to the end right.
And we need the like of
file in bytes so infile that tell G Very.
And then we go back to the beginning.
Right.
So in file this sheet
so says go to the end of the file tell me what position the current byte is at which basically just
gives me or current character is that and this just gives me how many bytes are in the file.
Right.
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
proper reading.
So this only gives us how many bytes are in the file.
But this doesn't tell tell us how many contacts we have.
Right.
So if we want to know how many contacts we have we have to say this number of contacts equals the length
of file in bytes over the size of a contact or eight.
So a contact has a certain size right in bytes.
And if we'd divide the length of the file and bytes over that size that will tell us how many contacts
we created initially.
And then here will create the capacity
as being the number of contacts
times two plus 10.
So we just kind of said how many there are based off of how many we wrote before.
And then we just need to set all our contacts.
Well first we need to actually set set pointer or actually knew the memory right.
What do your contacts because when we load it when we only load it once at the very beginning.
So when we load it this is set to no pointer.
So it's not actually pointing at any memory at need point to that a memory
city.
This and then we say contacts he'd got a number of contacts we know how many that is
in a capacity
capacity like that.
Now our context is it finally initialized now need to actually read it or read it.
Sorry.
Read should look exactly like the right for
and we read how many bytes are in the fight right.
Then just close the file
and return true that it actually read everything.
So that's it for that.
So are we done.
Oh we load the contacts right.
And all that.
So that's OK.
And the last thing we should do is say if
if we have loaded the contact or if we haven't learned the contacts we should kind of output that we
didn't
didn't.
Error eating file
the file name
it's
Annibale kind of return zero here because if we can't write read the file then we can't do anything
right.
So let's run this.
You.
It's so everything foul contacts up in.
OK.
So why why do you think that is.
Right.
So the reason is because the file doesn't exist.
And we said before that if you actually write to the file it'll create the file but we'd never have
written to the file.
So how do we create it.
Here's a little trick that we can do.
So I'm going to write one more function
in void and say create contact us file and copy this
and we're going to paste it here.
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
create the file.
And open tax file name stream Harry.
If not you can file is open so we couldn't open the file.
That probably means we haven't actually created it.
So we'll say off stream
file.
And we'll say oh file it open
again.
Street
Nery write and I would like to effectively make an empty five day and how you do that is this is out
file and insertion operator and we'll say flush and then close it
so flesh will actually
essentially write out nothing but this will still create the file.
So that's kind of a nice little trick you can do if you just want to flush the entire file.
So it basically just makes a 0 byte file is this.
So it opens it or does kind of flush meaning a just please just create it right and then close and then
otherwise otherwise you could open it.
So you should close it
and up here we will actually create its
if it's not created already.
And there's one thing that we should do after the loop is done.
Which is save the contacts
so if we quit we should just save them all
and we should also delete the memory right.
We haven't done that
today.
This is just good practice to do.
So no I think that's kind of it.
It's be missed anything right.
So let's read this and see what happens.
So notice we actually got to the main screen.
So it actually was able to load even though it probably has nothing.
OK.
So what happens if we display nothing happens.
Let's try to add something.
So please enter the first name Fred last name and and please enter a number
saved and see if that actually worked.
So Fred Flintstone then has the very first phone number in existence right which makes sense.
OK let's add something else
OK.
So there you go.
So we have a list of contacts that we can add to which
so flee all of that made sense.
And we found this kind of interesting this is really just a good review.
And it is also to illustrate to I guess more of the advanced students how to not always have to do a
top down design.
Right.
We can kind of do this piecemeal as it makes sense.
OK.
Which is which is usually the more common method that programmers will use sort of.
Right the usage code even though you don't have the code at all and then make sure that makes sense
and it solves the problem and then actually go back and implement everything that that you had you declared
before.
So that's kind of a more natural way and that's usually the more it's usually the way I approach problems.
Now I rarely do a top down design especially for really big projects you usually want to break a small
piece of it right down and just implement that and then test it and see if it's working.
And then kind of add on to it.
And and that's really b approach will take for the very final project in this course.
So I will see you in the next section.