So in the last lecture we learned how to create books.
Now let's make it so we can read those books.
So what I want you to do is create a new definition.
Call it option list books and do two decimal two.
So if they type two they'll have the books listed to them.
So we're gonna go in that case option list books.
And now I want us to create a function for handling that.
So if we go up to here handle create we're going to create a new function we're gonna call it handle
less
okay.
And now all this is going to do it's going to go through the books and list them out.
So I go strict be set strict book B which will hold up book and then we want to set the final position
to the start of the file
and then once we've done that that alone point to the Stop the file.
So the reason we need to do this is because if they decorate and we seek to the end then we need to
seek back.
Now in this case because our program ends as soon as they've created you won't have to do this.
But if our program was to continue running then you would need to be able to set back to the beginning
of the file.
So we'll just use this as good practice.
So now what I wanted to do is I want you to go f read and it's going to ask for a buffer so let's pass
and M some be the dress a b it's going to ask for element size so as to size of B and then finally it's
gonna ask for element count.
So let's do one and now we're gonna pass an F for our string and I will.
All I want to do now is do this f f rate equals 1 because remember we're reading one block one element
that is the size of our book.
So they still read that but and and if it reads it then then this will be executed right.
But it would make more sense for this to be a while because we want to read all the books to change
this store a while and then that will go through and read every single book until there's no more books
to be read.
So we should be at it now goes something like this.
Print.
And we should be at it go.
S and B title print F S and B that description we should be had to do that.
So if we now go back to our switch and type handle list and we're gonna compile that now.
Oh man dot c guys not CBP and then we're gonna run that file when it's compiled so we're gonna run that
it's gonna say into an option and option list books is number two.
So if we type to it less the godfather.
This is a good book Harry Potter.
This is a great book.
So you can see we've now read those books from there.
So what if you want the ability to select a book then what we can do is we can have it.
So each of these gets an index and then you can enter the index to be able to view more information.
But in order to do this properly we're going to need to use the heap.
So this is a good example of when to use the heap.
So what we do here we do integer index equals zero in here to index plus plus stack to increment the
index get rid of description because we don't display all the information here.
Now we do percent high dash and here to index.
So if we can pilot now you should be able to see if we press to 0 the Godfather 1 Harry Potter Harry
Potter.
So what we want to be are to use these indexes to view more information so how do we do that while we
can use my lock.
Now there's two potential ways to do this.
You can allocate some memory and then reallocate it on each iteration to increase the size.
You can look into the real lock function memory function if you want to learn about that as it was not
told on the memory lecture.
However we will just use standard Matlock for this.
So what we can do is first we need to find out how many books there are so let's create a function
just call it how many books
so all this will do this.
We now need to find out how many books there are and how you can do that.
Easy enough.
Seek to the end right.
And then do total size unsigned long.
Total size
FTL and pass an F that seat back to the beginning
and now the.
How many books there is the total size divided by the size of struck up and that'll give us how many
books there are.
Okay so you could also have some sort of header in your actual binary file and that could tell you how
many there are but this is just easy because we can just divide by the size right.
So how many books this this equation here will return that.
Because we get the total size of the file.
We divide it by the size of the book and then that is how many books there are.
Right.
So what we can now do we can say integer total books equals how many books
and then once we know how many books there are we now know how much data to allocate.
So just to struct book asterisk PTA equals struct book asterisk my luck my luck size of struct book
multiplied by total books.
You can also use calc if you preferred which will allow us to basically just do this and that will be
the size of the book and how many books there are.
And then it will do the multiplication for you but just keep things simple.
I like to use the my love just to show you guys that this is how it's done.
So the total size we're allocating on the heap is the size of the book multiplied by the total books.
Now now we can do this.
Men copy PTA PTO index I should say I'm an ampersand here because we're getting the address of this
index in the in the in the pointer because you can access pointers through indexes as well right.
So if this was 0 we're accessing Book 1.
If this was one where Xs and Book 2 and how that works is as you put an ampersand for access in the
point A and then you use the index as you would then that's basically saying I want to I want to push
forward and get get this book I'm not not the one at the beginning.
Okay.
So now we do that.
And then we do source.
So we do m some B size of B and now that should work fine just to test.
This works completely fine.
That's 2 4 inch I equals zero.
I blow total books I plus plus print f pi.
I mean f s
there you go.
Now that should work.
We run that now compile and run it.
And we can see Harry Potter.
If you put one in there.
Oh yeah.
The Godfather and high pass you can see his output at them correctly.
Which is good.
That's what we wanted.
So if you now move this pointer up here and just to PTI and we're going to put the top our file just
right next to file and we're gonna set this to no she's 0 and I want to do the same to the file pointer
as well
and then uh once we've done that it's added it all into memory for us.
So now we should be able to ask the user again another question which property you want to read.
So we'll do that now.
So we go uh print f choose a book and then we go scan F and I.
And here we do uh option Ampersand option and just up here we're gonna go in option equal zero.
So it'll install that the option they chose in the option right.
So now what we need to do we just need to do some bounds checking.
So we don't crash our program right.
So we say if.
Option below zero for option above total books.
It'll be above our equals actually because we started zero for the book count.
Then print f invalid book and we just do not line terminates.
And then we just do the turn minus one.
Okay.
And do it 10 0 down here if everything went fine.
Now we should now finally be at a view book.
So all you have to do is go the book PTA option that's all you have to do.
And if we now go up here we can go and create that view book function void view but struct book book.
And now we now should be able to do print f title sent us access SSN book title print f o print f description
percent s back session.
Book Description if you now compile that
you can see it s complaining.
That's because we need to put our ampersand in.
So we go back to a view book to an ampersand here because we want to get the address of this book.
Paul that main to says choose a book Choose.
Choose to invalid book.
Try that again.
Choose one Harry Potter.
This is a great book.
Choose 0.
The Godfather.
This is a great book.
Choose minus one invalid book.
So you've now made your program be able to create and read books which is excellent.
Congratulations.
So I think we said we were gonna put a flag to be able to specify whether the book had sold or not.
So we're gonna do that on the final part of this tutorial pop 3.