Hello, everybody, I would making a few quick videos covering each of the system calls that are remaining
for file manipulation.
The first one is do you remove system?
Call this system called delete a file, and that file is dependent on the path that you pass in.
And like the other ones, it returns an integer.
So what you're going to want to do is check the return value, and if it is negative, that means an
error has occurred.
If not, then it is fine.
Let's head over to a quick example in my virtual machine.
OK.
Here is the file that we left off on in the last lesson.
As you can see, all we are doing is opening a file called New File that takes tea and read and write
mode.
And if it does not exist, we will be creating it with these file permissions.
Here we are checking the return value of the file.
And then here we are, simply closing it and checking that value.
Now what if we want to delete that file?
As you can see in our director, we currently have a file called New File that let's just open up our
main tool page for remove.
And as you can see here, this manual page says that remove and removes a file or directory.
And here is the header file you need and also how to use the system call.
And as you can see, it just simply takes in a path.
So what we were going to be doing is how we done it before where we will be setting integer value.
It's due for why.
And here we can do y equals remove.
And then you file that as before we are going to be checking this value.
If Y is less than zero, you can just simply print off error.
And then exit that wound here on this program, it'll be creating a file, opening a file, sorry,
but it's already created, so it will be opening in read and write mode, then it will be closing the
file.
And then lastly, will remove the file when you quit out of this over here and save this right here
and do a C file node.
And then run this and then let's see, let's check the contents of our directory.
And as you can see, there's no file call, new file that test.
If this is a kind of confusing and you kind of want a better output, you could put print commands so
you can actually say file has been opened files and close file has been removed just to sort of you
could step to the process and also set break points and then check what's in the directory.
Now what would happen if there is no file that needs to be removed?
So let's say that the file we want to move is called something else.
Hello.
Let's see what it's going to do first.
Let's compile this now and run it says error.
And that error is occurring because there is no file called Hello Dot.
But by doing this, check right here.
This is preventing this from crashing our program.
It is simply printing an error to the screen and also exiting.
If we were to get rid of this and try to remove a file called Hello Dot, Hello Dot.
And that file does not exist, then we will run into an error.
And as you can see, nothing is printed out to the screens.
If you have a bigger program at hand, this will cause many issues because it might be a crucial thing
that should be happening but is not happening or that you coded wrong.
So in this example, the program did not crash.
But if you did want to remove a specific file and your program did not do that because you do not have
the proper check in and this returned negative value, but your program does not check that, then that
can lead to problems with the rest of your program.
OK, so let's head on over to the next system.
Call in the next lesson.