Hello and welcome to the next lesson in this lesson, we'll be covering the right system, call this
system, call writes and bytes from a buffer into a file descriptor.
And like the other system calls it returns an integer.
This integer represents the number of bytes written to the file descriptor.
And if that number is negative, that means an error has occurred.
And like the recommandé, it takes in a file descriptor, which this is the return value of when you
open a file, a buffer which holds the contents that you want to write to the file descriptor.
And lastly, the number of bytes that you want to write to the file descriptor, which usually is the
same length as the buffer.
So now let's head over to my VM and look at a file writing example using the right system call.
OK, now that we are at my VM, the first thing that we want to do, as always, is open the manual
page for the system call we're using.
And as you can see, this is the wrong manual page.
It's it's just for a right one, which sends a message to another user that is not what we want.
So let's try man to write.
And yet this is the right one that writes to a file descriptor.
It needs this library and this is how you call it.
It takes in the file descriptor of the file, the buffer and the account.
So the first thing that we need for the system call is the file descriptor, and that file descriptor
comes from when you open a file.
So let's first open the file that we actually want to write to you, and that can be done by as we've
done in previous lessons.
We can just do in file oops caps and file and then file equals open and we can name this and if we want
so we could stay in my file data to you.
And remember, this is the next where we put the flags.
So we want to open it for free and also read and write.
And if it doesn't exist, we want to create it.
And lastly, this is where we set the file permissions.
We're going to do zero seven five five.
We will have the read, write and execute permission, and the other two groups will only have the read
and execute permission.
And like we've done in previous lessons, we are going to want to check that this does not check that
this is not a negative value because if it is, that means an error has occurred.
Let's just do this real quick.
If it is less than zero error creating splash opening file new line and that will take care of that.
Let's go ahead and try to compile this so we can see what we have so far.
GCSE right one.
And it looks like we have an error, right?
Spelled it wrong.
And that is what we actually want, if we copilots.
Let's clear this up so we can see it.
And as you can see, the only thing that we have in our folder, our template right now is a eight hour
program, which is what the binary that we just compiled and our source code when we run edit out.
Let's see the output.
As you can see, it created a file called my file.
That text, which is a good sign.
It looks like I have an exit out there, but our file is on the right track so far.
So far we are opening the file called my file that you see.
But now what we actually have to do is right to the file.
Let's open up the man to write again.
So the next thing that we need is a buffer, a pointer of a buffer for this.
You can use either string, but just so we can stick with the using just see types in this lesson,
we're going to use a chart and here we can do this.
And then we write this to the file and we can say I am writing to a file.
And what we will want to do is how we've done it with others, just some calls it create an integer
for this.
And remember this value that we use that is returned when we call the right system call is the number
of bytes written.
So in this instance, I will be calling it the right size, just so I know that this is actually the
number of bytes written to the file.
Here we will call it Right size equals right.
And the first thing that it wants is the file descriptor.
We could pass in the file descriptor, which is right here.
File open return is the file descriptor so we can just call a file and then it takes in the pointer
to the buffer.
In this case, it is right this right.
This and remember, there are a lot of different ways he can set this up, but this is just how I will
be doing it for now.
And next, it takes in the count so the number of bytes.
So we basically want how many bytes this is.
And for this, I imported the string data library so I can easily just do a string length of write this
and that will basically return.
Turn the number of bytes containing in this string.
And as always, you should check the return value because if it is negative, then that means that an
error has occurred.
And I know I skipped this and some lessons, but it is something that you do need a check every single
time.
But just for making the code easier, sometimes I don't include it.
So error writing to file.
And then as always, just for a quick hand, we will do it if close.
And then the last thing we do want to follow is closed less than zero, then that means that an error
has occurred.
So we're going to call that an air.
Everything looks good so far, but first, let's go ahead and look into the missile that he and.
Just so we can make sure that nothing is there so we can do a quick cat, which is people use for,
it's actually for cooking catnip, but you can use it just to look in the files really quickly.
And here we can see that nothing was printed out.
So now let's go ahead and recap all our programs so we can see if our file does anything different.
And as you can see, this is the wrong files.
I'm going to remove this one just so we don't get confused.
Else so the only thing we have in here is our updated program, which is opening a file, writing a
file and closing the file.
Let's go ahead and compile that and let's run it.
And as you can see, it created a file called My Father to do a quick last minute, so they see that
there are 24 bytes in my file that.
And if you go ahead and count, how many are here, you will get 24 bytes.
But now let's go ahead and type in and see what's actually in here.
And as you can see, there are we did successfully write to the file, but we can just verify it with
this and open up my file that taxi.
And as you can see, we wrote to the file using a write system called.
Let's go ahead and just recap really quick with what we have done so far in our right system call example
so far we opened a file called my file that in read and write mode.
If it does not exist, we create the file with these file permissions zero seven five five and those
correspond to these file permissions.
So the use the owner will have read, write and execute, and the other two groups will have only read
and execute permissions.
Then we check to make sure that an error did not occur when opening the file.
Next, we created a buffer that stored the text that we actually wanted to write to our file, and then
we actually wrote to the file past it in the file descriptor, the buffer and the number of bytes that
we want to write to the file.
And then lastly, we checked the error to make sure that there was no everyone running to the file.
And lastly, we closed the file.
OK.
And I wanted to give you one more example, and this example will contain more file manipulation.
But when I say file manipulation, I mean that we will be reading from a file and then modifying that
text and then writing it to a new text file called modified text.
So I already outline the program a little bit just to make this video a little bit shorter.
But the general gist of this example is that we will be reading from a file called the random text text,
and this is the text file that we've used in previous samples, and we'll be reading 100 bytes at a
time.
And if that isn't even read, then we will be writing, even read to modify text.
If it is an odd read from random text and we'll be writing the 100 bytes to modify text that might be
a little bit confusing.
So let's just go ahead and start, get started, and maybe it'll help clear things up.
I already went ahead and opened the two files that we will be using.
The first file that we'll be opening is the random text underscore text.
That text will be opening and read and write note, and if it does not exist, we'll create it with
these permissions.
Next, we will be opening the modified text on text, and this is a file that does not exist currently
in our directory.
As you can see, we currently only have random text dot text.
This will be creating a modified underscore text that text in reading right mode.
And if it does not exist, it will create it.
So in this case, it does not exist.
So we'll create it with these file permissions of read, write and execute for all user groups.
Next, we are just simply check in if an error occurred when opening those files.
And if we just skip over a while loop, which that's what we will be implementing in this tutorial,
you will see that I am simply just closing the two file descriptors and seeing if either those are less
than zero.
That means error as occurred.
The main bulk of the program, which I want to talk about, was this while loop, as we've done in previous
examples before, we needed Wildl to iterate over the file that we were reading from in order to read
the entire file.
Let's just go ahead and get started with how we've done that before.
The first thing we want to do is create a recount for the file descriptor.
We're going to sort into a buffer and we want you to read 100 bytes at a time.
Let's go ahead and create the buffer called buffer, and we want it to be one hundred bytes.
We want to allocate 100 bytes.
The first thing we want to do is instead of just simply writing, read again and read Retreat until
it's in a file, we use our while loop while you recount.
So while we are reading bytes from the file descriptor, which file descriptor points to random text
while we are reading bytes from the file descriptor.
So while it is great in zero, we want to write.
So this will be code for logic, for writing two modified underscored text on TV.
Here we are going to be creating two.
We're going to create one false statements.
So if something else, something else in this example, how I want to just simply show a very easy of
file manipulation, I wanted to show that if it is, it even read, which means that it's the first
read or no sorry.
If it's the second read for three eight six read, then we want to only print, even read to the modified
text file.
If it is an odd read, we want to print the entire contents of the buffer to modify text that can be
done with a simple if statement.
If the number of reads if it's divisible by two, then we want to go ahead and write.
Even read and we want a new line there, and I believe this is 10 bytes long.
But it takes care of the even Reid situation.
But how about if it's an odd read, if it's an odd read, which means it's not divisible by two?
Then we want to write the entire contents of the buffer to the second file descriptor because the second
file descriptor points to the modified text that underscore our text.
So here we will simply write to the file descriptor, too.
We want to write the entire buffer to write the entire buffer.
We put 100 bytes here because that's how many bytes are stored in buffer, and we want to write you
the file descriptor, too.
And there are easier ways to do this, but just so we can see exactly what is going on, I want you
to print a new line and I know we can combine these two.
But in this example, just for simplicity, I want to just create a separate command to create a new
line in the file.
Our while loop is currently iterating over and over, so it will keep printing the same exact thing,
but we want to read the next hundred bytes in the next hundred bytes in order to do that.
We have to call the read system call again so we can do the same thing we did up there.
Read count and read from the file descriptor into buffer, and we want to read 100 bytes.
And as always, we should be checking all of these integer values.
But just to make the code look simpler, I'm going to keep it at that for now.
And here we want to increase the number of read, and I don't believe I declared the number of reads
out.
So let's go ahead and declare number of reads.
Currently, it's iterating over the entire file descriptor, which is pointing to random text, so it's
iterating over random underscore text that text and every time you iterate, it is increasing the number
of reads.
And what that is doing is changing which section of code it is going on, going into when it reaches
that, when it is, even it will just simply write, even read to the file descriptor.
And if it is an odd read, we are simply writing the entire buffer to the file descriptor, and it looks
like I forgot to put what file descriptor we're going to write to.
That is how we do that.
I believe the code looks all good.
Now let's go ahead and give that a safe and a quick GCSE for right to see and no errors occurred.
So let's go ahead and write it out and else.
And as you can see, it created a new file called Modified Underscore Text that text.
So if we do a quick list minus myself, we could see that in modified text underscore text that you
see there are two thousand four hundred forty two bytes.
And if you do the math, that kind of makes sense because every other read we're only writing, so it
is about half the size of random text.
So 4000 versus 2000, about half.
But if we go ahead and look inside of the modified text, we will go ahead and see that our code implemented
with how we want.
So it first starts off with an even read because zero and then it reads 100 bytes or writes 100 bytes
to the modified text, and then every doesn't even read.
And then it prints another 100 bytes, which 100 bytes.
And then it reads from the random text 100 bytes and it keeps going back and forth.
So what do you want to show you how easy it is to manipulate the text?
And also what we could be doing in here is modifying the buffers in here instead of just writing the
contents of the buffer.
We could change some of that text.
So instead of saying this, we could have made it say who at the end of the last five bytes could just
have your name in it or whatever, but just want to quickly show you that this is how we can do a quick
even or odd read.
Because if it's even read, it's going to print.
Even read odd reads.
You're going to print the 100 bytes that it read.
And as you can see, it's a little jumbled up, but that's because we manipulated the file that we just
read.
But in here, you can see that there are a bunch of even reads in here.
So it's basically every other 100 bytes you'll read you'll be able to see and even read.
There is a lot you can do with this program and a lot that you can move on from here.
But I just want to give you an example of how you can manipulate a file.
But if you look up project ideas based on fine manipulation, this will sort of give you a good understanding
of the flow and how to simply use this while loop and just use a conditional statements.
And within those conditional statements, you can write certain text or modify the buffer, modify the
buffer before you use it.
And as always, you want to make sure you close it.
That concludes the end of the write system call lesson.
I hope it was helpful, and if you have any questions, be sure to let me know.
I will see you in the next lesson.