REMOVE()

this function is used to delete a file or directory entry from the filesystem.

int remove(const char *filename);

 * filename: A string representing the path to the file you want to delete
 * Return value:
    - 0 on success
    - non-zero (usually -1) on failure
    
 * Deletes the file or directory entry specified by filename
 * On failure, errno is set (e.g., file not found, permission denied)
 * If the file is open, the behavior is implementation-defined (may fail on Windows, 
   may succeed on POSIX)
 * Only regular files can be deleted on most systems using remove()
 * To delete a directory, use:
    - rmdir() on POSIX systems (<unistd.h>)
    - RemoveDirectory() on Windows (<windows.h>)
#include <stdio.h>

int main() {
    if (remove("data.txt") == 0) {
        printf("File deleted successfully.\n");
    } else {
        perror("Error deleting the file");
    }
    return 0;
}

Last updated