C Program to Delete a File

C Programming Examples


This C program deletes a file which is entered by the user, the file to be deleted should be present in the directory in which the executable file of this program is present.

Extension of the file should also be entered, remove macro is used to delete the file. If there is an error in deleting the file then an error will be displayed using perror function.

Example:

#include<stdio.h>

main()
{
    int status;
    char file_name[25];
    
    printf("Enter the name of file you wish to delete\n");
    gets(file_name);
    
    status = remove(file_name);
    
    if( status == 0 ){    
        printf("%s file deleted successfully.\n",file_name);
    }    
    else
    {    
        printf("Unable to delete the file\n");
        perror("Error");    
    }
    
    return 0;
}

Program Output:

delete-file-c

Deleted file doesn't go to trash or recycle bin so you may not be able to recover it. Deleted files can be recovered using special recovery software if the files are not overwritten on the storage medium.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.