remove() Function

The remove() function in C stdio.h is used to delete a file from the file system. It directly operates on the file specified by its name, without involving any file streams, making it a straightforward utility for file deletion tasks.


Syntax of remove()

</>
Copy
int remove(const char *filename);

Parameters

ParameterDescription
filenameA C string containing the name (and optionally the path) of the file to be deleted.

The file name must adhere to the specifications of the running environment. Proper file access permissions are required for the operation to succeed.


Return Value

If the file is successfully deleted, remove() returns 0. On failure, a nonzero value is returned and, in most implementations, the errno variable is set to a system-specific error code.


Examples for remove()

Example 1: Successfully Removing a File

This example demonstrates how to create a file, then delete it using remove(). It checks the return value to confirm the deletion was successful.

Program

</>
Copy
#include <stdio.h>

int main() {
    // Create a file for demonstration purposes
    FILE *fp = fopen("sample.txt", "w");
    if (fp != NULL) {
        fprintf(fp, "This is a sample file.");
        fclose(fp);
    }

    // Attempt to remove the file
    if (remove("sample.txt") == 0) {
        printf("File removed successfully.\n");
    } else {
        printf("Error: Unable to remove the file.\n");
    }
    
    return 0;
}

Explanation:

  1. A file named sample.txt is created and written with sample text.
  2. The file is then closed to ensure all data is flushed and the file is accessible for deletion.
  3. The remove() function is called with the file name to delete the file.
  4. The return value is checked; a return of 0 indicates successful deletion.

Program Output:

File removed successfully.

Example 2: Attempting to Remove a Non-Existent File

This example illustrates how remove() behaves when trying to delete a file that does not exist, and checks for the error condition.

Program

</>
Copy
#include <stdio.h>

int main() {
    // Attempt to remove a file that does not exist
    if (remove("nonexistent.txt") == 0) {
        printf("File removed successfully.\n");
    } else {
        printf("Error: File could not be removed.\n");
    }
    
    return 0;
}

Explanation:

  1. The program attempts to delete a file named nonexistent.txt, which does not exist.
  2. The remove() function returns a nonzero value to indicate failure.
  3. An error message is printed to notify the user of the failure.

Program Output:

Error: File could not be removed.

Example 3: Removing a File with a Specified Path

This example demonstrates the deletion of a file located within a specific directory path. It follows the same procedure as previous examples, with emphasis on path usage in the file name.

Program

</>
Copy
#include <stdio.h>

int main() {
    // Create a file in a subdirectory (assume the subdirectory exists)
    FILE *fp = fopen("subdir/example.txt", "w");
    if (fp != NULL) {
        fprintf(fp, "File in subdirectory.");
        fclose(fp);
    }

    // Attempt to remove the file with the path
    if (remove("subdir/example.txt") == 0) {
        printf("File in subdirectory removed successfully.\n");
    } else {
        printf("Error: Unable to remove the file in subdirectory.\n");
    }
    
    return 0;
}

Explanation:

  1. A file named example.txt is created inside the subdir directory.
  2. The file is closed after writing to ensure it can be deleted.
  3. The remove() function is called with the full path to the file.
  4. The function returns 0, indicating successful removal, and an appropriate message is printed.

Program Output:

File in subdirectory removed successfully.