So using everything you've learned in this course when they were going to create a simple book program
that allows people to add new books to the program and pull pull which books are already created.
Now we're going to need a hexadecimal editor to do this because we're going to be storing binary in
our file and binary characters cannot be displayed in a text editor correctly which means that we cannot
use the text added to examine our file for me to explain it to you.
So we need to download hex editor neo so if you open firefox
and in Google type hex editor neo download and just you hate G.D. software they'll come here and press
download
and then open the installer
and just press install and now I'll say launch hex to Neo.
Just press close
and hex it adds to neo will load.
Just press continue on OK.
And this is our hex editor.
So now that we have hex editor neo installed now we can finally begin the coding.
So include the STV lib had the file which will give us access to the file functionality and now I wanted
to create a new file here.
I just call it data dump and up in is just an extension and we're calling adoption because for binary
it can be named anything really.
So we're now going to go file asterisk and we're gonna go F equals f open dot for SAS data dump in and
in here I want you to do R B plus.
Now I know we didn't discuss RB yet essentially with your mode all you have to do is put a B after it.
And while this will do it will open the file knowing that it's a binary file.
If you don't do that it will open it as if it's a text file and there's certain characters in the text
file that the functions will interpret differently whereas if you do B.
That means this is a binary file.
Don't do anything special that might make it difficult for us to read this because in text file you
have new lines and all that sort of stuff and the functions provided and C can handle those and you
don't really want that for binary files.
So use R B and plus here so that I'll open this file for reading and writing and we can now say F asterisk
f failed to open file return minus one and then he had just to return 0.
If you now compile that
we see nothing which means that it works successfully.
If you don't have dated up in if you didn't create it then you'll see that it said failed to open file
guys.
Make sure you create that first.
So where our program is going to store books.
So books are going to be a structure.
So let's make a strict code book.
Our books can I have a title which will be 20 characters.
It is going to have a description which is gonna be two injured characters and we also want a flag that
determines if it's sold or not.
But we can get to that later.
Let's keep it simple for now.
So title and description.
That is all we want to deal with right now.
So our program is gonna have a few modes but before we continue I want you to copy our file pointer
here.
Replace with F and just paste it up here because we're gonna be making some more function soon and we
want to be a to access that file.
Right.
So let's create a new function called void.
No integer handle create.
That's all I want to do.
And what we're now gonna do is we're gonna ask the use of some options of what they want to do but to
keep the code clean we should also move this into its own function.
So copy that and we're gonna go void no integer
setup file.
So all we're gonna do and I want you to paste that in there.
So that is going to be responsible for setting our file up basically loading it.
So if we now call set up a file here and I want you to check if setup file below zero then return minus
one.
So all I wanted to do.
And we also need return 0 in the setup file function guys.
Because if everything went OK we want to return 0.
So currently this should set up our file if we can pull out.
Now we can see we see nothing which means everything went fine.
So we're now going to ask the user to choose some options.
So let's not create a another function called Choose option the returns and interest you guys suggest
you return zero here.
That means everything went fine.
And then I want you to say if choose option below zero return minus one.
Okay.
If you right click and press format document by the way visual code will structure your code nicely
for you.
So in here now we're now going to ask the user to enter an option
that's all we're going to do.
And there'll be a few different options.
We're gonna have options for adding new books and for reading books.
So let's now go scan if I am we're gonna go option and we're gonna need integer for that as well and
put an emphasis on here to get the address.
So if you remember scan F. we'll read from the keyboard.
And in this case we're reading a number right.
So we are now going to enter a switch.
So when you switch option and these are the options they can have.
So at the top of the file I want you to define some definitions so go define option create book and
that's gonna be 0.
Define option read book.
And that'll be 1.
Now what I want you now to do is go case
option read book
and I just wanted to go print if you selected to read a book.
Excellent.
Now at the bottom here I want you to go to fault.
I wanted to go print Princess.
Um.
Uh.
No.
Invalid option chosen.
I just wanted to return minus one.
So if they enter an invalid option we're gonna return minus one which will cause us to leave the program
because we check if the option was bad we would return minus one and leave the program.
So just compile that now and run it into an option Celeste to 50 invalid option chosen.
Now let's do zero invalid option chosen 2 1.
You selected to read a book so that's how that works right.
Let's now go.
Case option
great book because that's what we really care about.
Now we can come back to the reading option later.
So go princess if you created a book and also put the new line terminates here as well.
Guys
we now compile up run that into an option zero.
You created a book and two option one you selected to read a book to invalid option chosen.
K. Excellent.
So we are now going to call our function we created our handle crate in the crate book.
So get rid of the print f
handle create
and this will output a result.
So at the top here I want you to go integer.
Oh yes equal zero.
Then adhere to our yes equals handle create.
And I say if our yes below zero return our yes.
So if handled crate returns and error let's return from here.
So if we now go back to handle create we can now add some more options print f and to the books name.
So what I now want you to do is above this print f just go struct book B for book.
So we create a structure about book copy.
So what I now want you to do is go a new line.
F gets B that title and I wanted to do.
Size of beads up title and then here LCD.
And so while they still do this I'll read from the keyboard and store a maximum of 20 bytes into our
book's title and obviously size of gets us the size of that title variable in the structure which is
20 bytes because it's an array.
Remember if this was a point the size of would not work in this case because size of gives us the size
of this variable which is 20 bytes because char is one byte right.
So.
So that ship at the title they entered into our book's title and we don't need to do an ampersand here
because remember arrays automatically return their address when you reference them.
Now if we now do print f book name a sentence backslash and beat out title and this will this will output
the name they entered.
So if you now compile and run that you'll notice a problem enter option 0 0.
What's this.
It doesn't wait.
F gets doesn't wait why is that.
Well there's a reason for that.
If we go back to our Choose option scan f only reach the integer and when you enter 0 it does give 0
but it also has a new line character that you can't see because as soon as you press enter it creates
a new line.
Right.
So what the f gets then does it then reads that New Line and assumes that's what the user entered.
So as a way around that I have to scan f just to get char.
And what that I'll do that will read the new line that they they entered by pressing the enter key.
If we now compile that again.
So make sure this get char below scan scan F and choose option.
We now run that again enter option 0 enter book's name.
I don't know Wizard of Oz.
There you go.
Book Name Wizard of Oz that worked so that out puts that as expected.
So what we can now do.
We can now ask for the description.
So let's now say print f and to the book's description and then we now do f gets beat up description
size of beat up description SCADA.
And then we can then do print f Book Description percent s back session beat out description and that
will that will allow them to enter the description and they'll output that description to them.
So we now compile that again enter option 0 4 right.
Uh hello book.
How are you book.
And you can see that works perfectly.
So on a tight book title and description and now both stored in book.
So now what you can do is you can write that book to the file.
So what we now have to do is write the file.
So just go f write ampersand B because we're gonna write the entire structure to the file and ask for
an element size.
Just put size of B an element count one and then for the file per F and while this will now do this
will write our entire structure to the file because that the element size is the size of structure.
We'll write one block of the size of our structure.
Therefore writing our entire structure right and we can use f conditions here to check if that write
failed.
But I'm not gonna do this here because I want to keep it really simple for you guys.
So if we now compile that now GCSE main Delta C does show Main and we run me to 0 Buck's name.
Hello Brooke.
How are you.
Press enter and now that's written into the files so we go back to data up in it says this is a binary
followed you want to open it do that and you can see that it's written off file and if we now click
on our file browser and we're gonna we are going to open it with NEOs so if you right click on Data
add up in and click edit with hex editor new and what will happen is load and you can see.
Hello Buck.
How are you.
So you can see all of this is stored but what is all this garbage by here.
Well we didn't initialize our B by setting it all to alter zero.
We didn't do that and that's why this is happening.
So what we need to do we need to set all of this memory and b to zero so so that we see zeros instead
of random stuff here because this stuff here is the stuff that is on the stack at the time we create
b because we don't set the entire array.
There is some junk leftover on initialized bytes if you will.
So all we can do is if we scroll up we need to include the string dot Hage had a file and then that
give us access to many asset and we can then do set and a class for destination.
So just put ampersand B the address of B A last for a value the value we want to set this to so do 0
and then do size of B so well that'll do is that'll set the entire B structure to 0 to nulls and if
we now close close hex idea then you make sure you do that.
Otherwise it won't last to open it again and then go GCSE may not see dash or main.
I want you to run main again
and a last for an option do 0 enter the book's name happy book.
How are you.
And now if we go back.
Is our pin with our hex as a new one small right click edit with hex editor Neil you can now see that
it looks a lot better all the bytes we haven't initialized are set to zero because we told it to not
decimal 0 it is set to binary 0 it isn't the decimal equivalent of zero.
Guys remember the ascii table so all of these and now zeros which is what we wanted.
So all of these bytes to onset are basically part of the description and title that we didn't need to
set because the description wasn't long enough or the title wasn't long enough.
Now we've store one book in dated up in successfully congratulations.
So if we now run that again unless store second book and we'll call it I don't know the Godfather or
whatever.
This is a good book.
And if we now go back text it is a neo again.
Then we open it
you can see that it's overwritten.
So why did it overwrite it.
Well because we didn't seek to the end of our stream.
So the default pointer is pointing at the start of our stream at the start of our file so overwrite
still one.
So what you can do to do that.
Just before F right do f seek and we're gonna say f f for the file stream it's gonna ask for a lot of
such as busy row and we're going to seek at the end and this opposition is at the end of our file.
We now compile that again and run that now into 0 and we're gonna try and hide Paul the didn't spell
that right.
Oh well this is a great book and if we now go back and we right click to open in hex it is an error
again.
You can now see it works as expected.
We have two books here.
We have the Godfather and we have our Harry Potter book here and this is all stored as binary guys writes
Excellent.
So we've now got our curation functionality.