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

I am trying to delete a file in a c program. Let's assume the file is in the current directory of the source file. I searched a lot but didn't get any solution. Everyone suggests using the function remove()

.

Here's 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 a way to delete the file without using the function remove()

. I want to code by hand without using any other built-in stl function.

+3


source to share


5 answers


+8


source


You can check this answer . You should try to read a systems programming book where you can learn about the use for example INTERNAL_SYSCALL

.

You can perform the functions mentioned in messages like unlink()

etc.



EDIT: Actually you will use the system call in some way at some level. You are probably trying to perform a file delete operation from different levels of abstraction. (The Remove () system call will also use INTERNAL_SYSCALL, which is nothing but a system call).

Now deleting a file from a low level does not mean that we are erasing something. We just think of space as free space (free pool of memory) and then any metadata associated with that file is freed as well. To do this, you need to implement a filesystem that allocates memory, removes it ... using device-level instructions.

+2


source


Call unlink

for files rmdir

for directories. You can easily check which file is using stat

and then call the correct function.

struct stat sb;

if (!stat(filename, &sb))
{
    if (S_ISDIR(sb.st_mode))
        rmdir(filename);
    else
        unlink(filename);
}

      

Include <sys/stat.h>

for stat

and S_ISDIR

, <unistd.h>

for rmdir

and unlink

.

Oh, and for your comment:

All of you did not understand my needs and requirements. I know it is possible to delete a file using standard library functions like remove (), unlink (), rm (), etc. But I want to code by hand without using the inline function.

Have fun reproducing detached source code .

+1


source


I think you need to know unlink()

. It remove()

calls internally to delete files unlink()

. See the man page for details .

However, first I suggest you change gets()

to fgets()

. Also, there int main()

should be int main(void)

.

0


source


Use system ():

PART I: (Delete File)

#include <stdio.h>
#include <string.h> 
int main()
{
    FILE *fp;
    int delete_status;
    char path[100],order[]="del ";//del for delete file, if change del to rd is delete folder(**Code at part 2)
    printf("Enter a path of file to delete it: ");
    gets(path);
    strcat(order,path);//Order
    fp = fopen(path,"r");
    if(fp != NULL){//Check file whether or not exist
       fclose(fp);
       system(order);//Del file
       printf("Delete successfully");
    }else{
       perror("ERROR");
       fclose(fp);
    }

    return 0;
}

      

For example, you want to remove 1.txt. Then you can put program c in the same file and then intput 1.txt or enter the whole file path (for example C: \ User \ Desktop \ 1.txt)

PART II: (Delete folder)

#include <stdio.h>
#include <string.h> 
int main()
{
    FILE *fp;
    int delete_status,i = 1;
    char path[100],order[] = "rd ";//del -> rd

    printf("Enter a path of file to delete it: ");
    gets(path);
    strcat(order,path);

    system(order);

    return 0;
}

      

-2


source







All Articles