Welcome.
So now we are going to add a flag which basically says has this book been sold.
And I want to show you how to do this because we're going to use up it.
Why is operations.
Because uh why not when I explained that and the bit why is lecture.
Um I explain to you how I work but I never showed you a scenario where it could be used.
So it makes sense to do it in this project.
So what I need you to do is I need you to go at the very top of the file.
Typedef unsigned char book Flags.
So remember typedef allows us to define a type and give it an alias name.
So book Flags is is essentially an unsigned char RIGHT.
SO IN HERE JUST TO BOOK flags flags all you have to do all you have to do.
And now just above book Flags to define book flag sold and.
And that's going to be defined to zero be one two three four one two three four get rid of the final
zero part of one.
So it's going to be a binary one.
Now you can also represent this like that or like that.
But when I work with bit masks This is called Bit bit mask and by the way what we're doing right now
with these book Flags is bit masking.
That's that that's the technical term for this but um I always prefer to write it as binary.
So it's very clear which flag is being set but that's up to you how you guys want to write that.
A lot of people like to do hexadecimal when I work with bit masks I always use binary because I can
clearly see which bat is being set.
Maybe it's this bit this the uh that's the flag right.
You know it's easy to see so.
Okay so once you've done that uh make sure there's no semicolon there.
By the way I have a feeling one of you might have done it.
Let's now scroll down and in our book create because by the way you're gonna have to delete this file
and recreate it.
Because we change the structure so we won't line up correctly.
But anyway back back to hands to create them.
What I now want to do is I want to ask them.
I want to ask these guys.
Has the book sold
that's all I want to say.
So what we're now gonna do is ask them yes or no.
So now we're gonna go um char c equals zero and we're gonna go uh scan f percent C and we'll get and
Ampersand C so it'll read.
It will read one character that they type.
So we're gonna be looking for a yes or no value.
So here we can say um F C equals equals Y or C equals equals lowercase y.
Then what we want to do then is is set the book Flags.
So we do that by going B.
Dot flags or equals um.
And it's a bit wise all remember.
Um we're gonna go to the book flag sold and you know I mean I'm assuming you followed through all of
the lectures because I'm not really explaining this because I assume that you know because you followed
the course all the way through right.
And I I'm just basically using what you've learned to help people the project so you can see where you
would practically use these skills.
So if they enter yes then the book the books flags is marked is sold because we pit mass that with a
bit wise or so.
If you've forgotten what that is look up a bit wise or you'll understand how this works.
The binary here is odd with the binary in our flags variable.
Now then what I now need to do is in our view book I need to say if Pop flags Ampersand book Flags sold.
So obviously this does a bit wise and on the flags with book Flags sold operator and obviously the result
here will be above zero.
If that flag is set.
So this is why this is true because it is non-zero.
So the if statement runs because our book flag is sold is set.
If our book flag so wasn't set then this would obviously return to zero.
And therefore this code would not run because zero is false and false does not allow if statements to
run.
So we're going to go in here now print f this book was so sorry just apologize to them or whatever.
Maybe they wanted to buy the book.
And if we now go to our uh uh at our command prompt and we're gonna see these into this folder to compile
this and we're gonna go in here we're gonna go Main and we'll get the GC chasing main dot see.
Show me and then we're gonna run main it's gonna send him an option too.
Oh wait a minute guys we need to delete this file first.
Otherwise this is not gonna read the data properly.
Uh because our data dump in doesn't contain the flags variable right.
So the data stored here won't be to be read back correctly so visually that and we're gonna create a
new file called did it up again just like that okay.
And now we can finally compile them on the program so compile it run it.
I'm gonna use option zero.
So he's asked me for the book's name I have to say hi Prada.
Uh this is a good book.
Has the book sold.
Yes.
Okay.
Good.
So if I now geez if I now do Maine again and I use option uh 2 it says choose a book so I choose Harry
Potter and it tells me this book was sold.
Sorry you say so I run mine again and I do zero and two books neighing godfather.
Uh great book.
Now I'm gonna say that it wasn't sold and I find that one that we do two here one we don't see that
this book is sold because that flag is an asset but if we do to zero again.
Hi Paul.
This book is sold.
Sorry.
So there's one more thing we need you to do.
Which I left out until now.
And that is we need to free our book points.
And we need to free our file.
Otherwise we have a memory leak now because our program ends as soon as we've entered this in the operating
system or clean clean it up.
But if this code was to keep running then our memory leak will present will persist.
Which is not nice.
So what we're gonna do in our handle lest you see our pointer here after view book we're just going
to free it.
So we're gonna go free PCR.
That's all I want you to do.
And then a numbing function.
I wanted to do f close F which will close our file.
Pointer don't do free on the file pointer f close handles all that internally for you and it closes
the file correctly.
Uh okay.
So back to our Maalox.
So yeah you can see we allocate the memory here.
We output the book and then we free the memory that we allocated passing it back to the operating system
so it can recycle that memory.
Now we we didn't have to use my clock here but the reason I did was because I wanted to show you a reason
to use it.
There are many good reasons to use my lock and the heap.
However I could have got away without it.
In this program if I wanted to another alternative to do this without the Matlock memory function would
be to pass the index into some sort of function that would then seek to the file it would load up that
particular element and an output that to you.
That's how we could have avoided using the heap entirely.
However I did want us to practice some Maalox skills again which is why we did this and this is a perfectly
acceptable solution so long as data doping doesn't get too large because obviously here we load absolutely
everything and.
Okay.
Well congratulations guys using everything you've learned you've built a little uh book program.
Congratulations and using the skills you've learned you can now take this a lot further.
A lot of what I've told you is used all over see regardless of which field you're in.
You could be in kernel development for example a lot of this same stuff is still used such as memory
allocation.
The functions are named a little differently but it's still the same concept.
So what I'm trying to say is you can take what you've learned here now to any field of programming that
you like.
It's the same concept all around.
So I'm I hope that you enjoyed this video.
And uh congratulations on your project.