RMDIR()

this function is used to remove (delete) an empty directory from the filesystem.

int rmdir(const char *pathname);

 * pathname: A C string representing the path of the directory to remove
 * Return value:
    - 0 on success
    - -1 on failure (errno is set)
    
 * Deletes the directory specified by pathname
 * Only works if the directory is empty (no files or subdirectories)
 * On failure, devs can inspect errno for the reason:
    - ENOENT: Directory doesn't exist
    - ENOTEMPTY: Directory is not empty
    - EACCES: Permission denied
#include <stdio.h>
#include <unistd.h>   // for rmdir()
#include <errno.h>
#include <string.h>

int main() {
    if (rmdir("myfolder") == 0) {
        printf("Directory deleted successfully.\n");
    } else {
        printf("Error deleting directory: %s\n", strerror(errno));
    }
    return 0;
}

Last updated