The next five manipulation system call that I want to cover is.
Currently, we are only able to read or write text from the beginning of the file in order to add text
anywhere within the file, say the beginning of the file or middle of file.
We need to use el-Sheik to reposition where you will be reading or writing the bytes to or from a file.
In simple terms, we are basically changing the location of the reads or write pointer of a file descriptor,
and we will give an example that will clear this all up.
But as the read and recommend, it takes in the file descriptor offset and hence which will be covering
what this is in the real example.
So let's head over to the VM to see and see example in here.
We will be writing to the middle of file into the file and beginning the file using the LCA system call.
OK, now that we are in my van, let's go ahead and do it.
Example the first thing is always is open the manual page for LC.
And this is the one that we want this see which repositions do you read?
What right to file offset?
You need these libraries in order to use it, and this is how you call it.
And you can see that we need to create a file descriptor it takes in the offset and an integer called
W hat.
And as you can see down here, it declares what w hence can be.
It can be sick.
Set C cur and seek.
And we'll be doing an example for each of those.
So before we get started, let's just try to get up so we can actually call our LC commands.
The first thing that we need to create is a file descriptor for the file that we want to be writing
to.
We will be using the same file we use in the previous previous example called random underscore text
that text.
We will go ahead and start creating this file descriptor so we can.
File descriptor equals open, random underscored text up.
We want to open it, and for now, we'll just be you doing it the same way you've done in the past.
Even though I know that it's already created and there are different ways to do this, but I just want
to keep consistent what we've been doing so far.
And we just want to check that if an error has occurred, so if the file descriptor is less than zero,
then we can go ahead and print error.
OK, so now that that is all set up, we can do the next step, which is writing to the file.
For now, we will just do the how we've done it before, where we simply type, right?
And for now, I'll be skipping the integer check where we see if an error occurred just because we were
doing quick testing.
So all I want to do right now is write to the file right to the file descriptor.
And again, I'm a creative something called a write this and then this will be right the length of write
this, which are the number of bytes we want to write.
And here we will do it like we've done before because you can't create a new line just so we can actually
see where we actually writing.
I am writing to the file starting.
At the end or beginning.
When we go ahead and compile this, let's see what happens to our modify our random tax credit checks,
and as always, we want to make sure we are closing this file descriptor just to make sure nothing goes
wrong.
Let's go ahead and check this LLC and there is an error with our error nuance our copilot.
And if we open up our random underscore text and text, and as expected, we are writing to the file
starting at the beginning.
That is the standard right command.
However, what if we want to write to the middle of the file at the end of the file or the hundredth
byte in the file?
And that is when Elsie comes into play.
The first thing that we can use when we're using LC is the main part that we are using is the directive,
which is a or C CR or seek.
And so let's go through an example for each of those.
First off, we will be covering these six set.
So the way the look works is before your write command, you want to use the LC command, create more
spacer.
We want to use LC.
And here is when you pass any file descriptor that you want to add the offset to of where you're writing.
Next, we will be putting in the offset and this example, the file is set to offset bites.
For now, we will just put in an example of let's just do 10 bites.
Then and then lastly, seek underscore set.
Before I go into any more explanation, let's just go ahead and compile our program, run it.
And let's go take a look at you, random that text.
So you're right, it's been.
So now what it actually did was it started writing our text at the 10th byte, so if we count from the
this is what was here before a count of one two three four five six seven eight nine and then the new
line is 10.
So started writing the contents that we want to write at the 10th bit and that was done by using the
Sikh set.
We could change this to any map with the changes to the hundredth bit, and we can change this to.
I am writing to one hundred.
But let's go ahead and save this.
Compile it.
Run it, open our file.
Oops!
Open our file round one.
And as you can see, let's look for the text that we just added in.
And right here I am writing to the site.
This is how you were able to write two different parts of the file, depending on what you want to do.
But let's go ahead and look at the other options that you can do with LCD.
The next one is called Seeker.
The file also is set to its current location, plus offset bites.
This really comes into play when you're iterating over a file.
But when we are just opening the file and writing to it, we can actually just use six set because as
of now, the corner is at the start of the file.
So no matter what we do here, it will end up the same.
But we could just simply changes to see Curve.
Just if you want to see an example and we can just start this ad, let's just start at the first byte
just so we can see what's happening and we can to say hello, world changes to anything you want.
And as before.
Let's go ahead and compile it and brought it and get it.
And as you can see on the first byte, we started writing Hello world.
And that's what we've done before.
But you could change us to anything you want, depending on where you want it positioned.
And let's go ahead and look at the next one.
The next one is seek and be file offset is set to the size of the file plus offset by it.
This is when you want to write to the end of the file or past the end of the file.
First, we will just write to the end of file file, changing it to zero and seek underscore end hello.
Or we just call it goodbye world.
In this example, and we will see that when we go ahead and compile it, run it and it, take a look
inside a random text, random underscore text activity and we scroll to the bottom.
We can see that it says goodbye world and we have the new line there, which is why it's there.
The last example we want to do is do an all seek to the end.
And as I showed before, you could just keep this at zero bytes and it will just go to the end file.
But if you want to go past the NFL, you can put anything you want here.
Say 10 bytes, pass the end.
And when you do a compile, DCC looked at it and you do it out and you open random underscored activity,
you will see that there is an error occurred.
That's because we are trying to write to somewhere that did not exist before.
But as you can see it, put in 10 bites right here and then roared our goodbye world.
That's not a good way to do it, but I sort of wanted to show you how that is actually working behind
the scenes.
It works fine if you just keep it a Sikh.
And so that is how you go ahead and write to different parts of the file using the Sikh, said Sikh,
Kerr and Sikh.
And now this is the end of the file manipulation portion of this course.
In the next lesson, we'll start covering processes and we'll move on from there.
So I will see you then.