8.1. Unlink a File
Table of Contents
There are several standard terms for deleting a file, which are
unlink, delete, remove and erase. The system call to delete
a file is unlink()
because that term accurately describes what
happens. Deleting a file removes the pointer to the file, but the
file remains unchanged. Similarly, removing an address on a map
doesn’t affect the actual building. Other APIs or GUIs use terms
to describe what the sees user, such as remove or erase, instead
of what the code does.
Both Windows and POSIX use functions
unlink
andremove
to delete a file.The POSIX function remove() is at a higher level because it calls
unlink()
to remove a file andrmdir()
to remove a directory. You can see the additional functionality in the POSIX remove source code.The Windows functions of
unlink
andremove
functions have the same prototype and presumably do the same thing (not verified).
- unlink
Deletes a file
// returns 0 if successful; Otherwise returns -1; int unlink( const char *filename // Name of file to delete. );
- remove
Deletes a file
// returns 0 if successful; Otherwise returns -1; int remove( const char *filename // Name of file to delete. );
Note
The file handle must be closed to the file others unlink
will return an error.
Template Code
Required Include Files
#include <stdio.h>
Basic Usage
char *filename = "delete-me.txt"; // unlink() or remove() int unlink_status = unlink(filename); // Check for errors // 0 if the file deleted successfully // -1 if an error occurred
Task: Delete a File
Create a file called lab8.1.c
for this task.
First, use unlink
and then test your code using remove
.
Add error handling to
unlink()
by evaluating the return value.Execute the code using an invalid file.
Verify that your program prints an error message with the PID.
Expected Output
Unable to delete file 'delete-me.txt'
Next, create the file.
Execute the code again.
Verify that the file deletes and
unlink_status
has a value of0
Expected Output
Successfully deleted file 'delete-me.txt'
Replace
unlink
withremove
and verify that both functions operate identically.
Hint
This method is a hack, so don’t use it for anything besides testing or as a developer tool.
You can create a file using
echo
in the command lineecho "some text" > filename.txt
You can wrap the command in function
system()
to run a command from your code.system("echo \"some text\" > filename.txt");