OK, the next system call that I will be covering is the reed system, call this system, call reads
and bytes from a file descriptor into the buffer, as you can see up here.
This reed system call takes NFL descriptor, and this file descriptor is the one that is returned when
you open a file.
It takes that file descriptor and it reads a certain number of bytes from it and places the output into
the buffer that you specify here.
And as always, you want to check the return value of your system call to ensure that an error do not
occur.
If not, it will return the number of bytes read, and this will be useful when trying to read an entire
file or do certain types of file manipulation with your file.
And now let's head over to my VM to do some file reading using the read system call.
OK.
In this example, we'll be reading from a file called random underscore text, not text.
And if we were to take a look into this file, we could see that, yes, there is random text in here.
We are going to be trying to read this text through our system calls.
The first thing I would want to do in our program is open the file, the way to open the files, how
we've done it before.
We want to do it our entire file descriptor.
Let's just define that for now, file descriptor equals open.
And then the name of our file, which is random on a short text activity.
And then we want to set our flags, which are going to be the same that we've done before.
Oh, underscore.
Read and write.
So we want to open for read and write purposes, and we want to open well.
In this case, we want it for read only so we could do it like that.
And then lastly, we can set the file permissions two zero seven seven seven because everybody can have
permissions for this one.
And as before, you want to check the value of the return.
If it is negative, that means an error has occurred.
So here we can do what we've done before and previous lessons.
Error.
And exit.
So the next thing that we want to do is physically read from this file.
And just for help, let's open up our man read.
And here is the correct mental page for read.
It is read from a file descriptor, so we want to do this kind of call.
So the first thing we want to do is set this equal to an integer.
And in this example, let's set entreat count and then we can do read count equals read.
And then next, it's taken in the file descriptor, so let's pass in the file descriptor of the file
we opened, which is empty.
Next, it wants to take in the buffer.
For now, I'm going to say buffer and then count.
So in this example, let's just read the first hundred bytes.
But now let's first.
We need to create the buffer.
Let's create a character buffer of 100 elements.
This is where the bytes that are read are going to be stored.
And then here we could simply just put buffer.
Now we want to run our program.
Everything will run fine, but we won't know if anything happened and we won't know if it happened properly.
So as always, we want to make sure that we include a statement to make sure that it was read properly
because if it is negative, then that means that the read did not happen properly.
So here's if less than zero, and for now, I will just put print off bearer.
Exit one.
So now let's go ahead and run our program and compile our program and run it, GCSE reads See?
And let's go ahead and run it.
And as you can see, there were no errors.
I'll put it, but nothing useful was really output.
It's here.
I want to first explain the recount.
If it is less than zero, then we print an error.
But if it is not less than zero, remember that the read system call returns the number of bytes read
if it is not if no error occurred, that it read a certain amount of bytes that we want to do is just
let's do a print f here four and then this is an integer, and here will be the recount recount.
So let's just print the recount so that we can see a better idea of what's happening in our program.
So let's see it and run it.
And it looks like I forgot to give it a new line.
But as you can see right here, it read 100 bytes from a file.
But what if we wanted to print the actual bytes that it read from the file?
What we will need to be doing is the same thing.
We haven't really covered in this lesson, but we're essentially just doing a simple print F. But mine
is going to be a little bit modified, but I could go more into detail if you want with this.
But this is how we are simply going to be printing these string and here and then we want to print the
read count and then buffer.
I want to take this is one line to make it easier to understand.
And here when we save it, it's just clear this if we have a fresh start and compile it, and it looks
like an error has occurred and and here we are going to compile it, clear it and let's run the program.
And as you can see, here are these bytes that it read it read.
And if you count this, this will be 100 bytes behind sooner dining.
So window and right, if we open up, she edit random text.
This will indeed be the first 100 bytes of our program up to here.
And you could do the same thing by changing this to any amount of bytes.
So if we only want to read the first 10 bytes, we can change it to 10 right here.
Save this.
Compile it and then let's go ahead and run it.
And as you can see, it's only reading the first 10 one two three four five six.
Don't forget, spaces count as characters.
Seven eight nine 10.
The next thing that I want to make sure that we are actually doing in this program is ensuring that
it is.
The file is closed once we are done, but something that there is a shorthand that we can be doing instead
of setting Int X and then X equals closed file descriptor.
There's a shorthand to do that, and that is to do it like this.
You do if close.
And in this example, when I close your file descriptor less than zero, then we can run the print statements
in error.
And then also we can exit one.
This saved us a line of code in this example.
But if you are doing a lot of read and opening a lot of files and running through a lot of files, then
this will become really helpful.
So you do not have to explicitly declare this and also write it.
So this is saving two lines of code, which might not seem like a lot, but it does really clean up
your code a lot and shorten it.
The next thing that I want to show you really quick is how you could read an entire file and that is
done by using a while loop.
It is like we've done it before we use the read system call, except now we are iterating over the root
system.
Call using our while loop this while loop is essentially checking if you recount to make sure that is
greater than zero.
And just a reminder, this recount is are the number of bytes that it read along, as the number of
bytes are read are greater than zero, then it can print the buffer and then also keep on reading the
file, and it will keep going until read count is equal to or less than zero.
And this will happen when it reached the end of file or if there was an error.
Let's go ahead and compile our program.
And now let's go and run it.
And as you can see it, print it out.
A lot of text counsel, but this is the entire contents of the file random underscore text start to
see.
So that is the end of this lesson, and I will see you in the next one.