Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Without using remove() function How to delete a file in C program

Tags:

c

file

I am trying to delete a file in c program. Assume that the file is located in current directory of source file. I have searched a lot but didn't get any solution. Everyone is suggesting to use remove() function.

Here is my source code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp;
    int delete_status;
    char del[50];
    printf("Enter a file name to delete it: ");
    gets(del);
    delete_status = remove(del);
    if(delete_status!=0) {
        printf("File can not be deleted!\nFile does not exist in current directory\n");
    }
    else printf("File %s has been deleted successfully!\n", del);
    return 0;
}

Is there any way to remove file without using remove() function. I want to code manually without using any other stl built in function.

like image 824
Shahidujzaman Shahid Avatar asked Dec 11 '22 23:12

Shahidujzaman Shahid


1 Answers

You can replace remove() with unlink() (for files) and rmdir() (for directories).

like image 164
mfro Avatar answered Apr 28 '23 03:04

mfro