BilimEdtech Labs

Labs:

  • Cloud Computing Labs
  • NASM Assembly Code
  • C Programming
  • Operating Systems
    • C System Calls: Overview
    • 1. Computer Boot Sequence
    • 2. Simple IO in C
    • 3. C Fundamentals
    • 4. Reading a File using System Calls
    • 5. Writing to a File using System Calls
    • 6: Creating a Process in C
    • 7: Terminating a Process in C
    • 8: Deleting a File using System Calls
      • Lab 8: Overview
      • 8.1. Unlink a File
        • Template Code
        • Task: Delete a File
      • 8.2. Empty a File
    • Reference Guides

Workshops:

  • Workshops
BilimEdtech Labs
  • »
  • Operating Systems »
  • 8: Deleting a File using System Calls »
  • 8.1. Unlink a File
  • View page source

8.1. Unlink a File

Table of Contents

  • 8.1. Unlink a File

    • Template Code

    • Task: Delete a File

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 and remove to delete a file.

  • The POSIX function remove() is at a higher level because it calls unlink() to remove a file and rmdir() to remove a directory. You can see the additional functionality in the POSIX remove source code.

  • The Windows functions of unlink and remove 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.

  1. Add error handling to unlink() by evaluating the return value.

  2. Execute the code using an invalid file.

  3. Verify that your program prints an error message with the PID.

    Expected Output

    Unable to delete file 'delete-me.txt'
    
  4. Next, create the file.

  5. Execute the code again.

  6. Verify that the file deletes and unlink_status has a value of 0

    Expected Output

    Successfully deleted file 'delete-me.txt'
    
  7. Replace unlink with remove 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.

  1. You can create a file using echo in the command line

    echo "some text" > filename.txt
    
  2. You can wrap the command in function system() to run a command from your code.

    system("echo \"some text\" > filename.txt");
    
Previous Next

© Copyright 2022, BilimEdtech | CC BY 4.0 | Licensed under the Creative Commons Attribution 4.0 International license.

Built with Sphinx using a theme provided by Read the Docs.