Linux 如何在C程序中不使用remove系统调用来删除文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16564323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:56:44  来源:igfitidea点击:

How to delete a file without using remove system call in a C program?

clinuxubuntu-12.04

提问by Datta

I've been curious how remin Linux works and trying to write my own Ccode that can delete a file but when I searched for the answer, I only got the programs that were using remove()system call.

我一直很好奇rem在 Linux 中是如何工作的,并试图编写自己的C代码来删除文件,但是当我搜索答案时,我只得到了使用remove()系统调用的程序。

Is there any other way of doing it without using system call like writing your own code to do the job?

有没有其他方法可以在不使用系统调用的情况下做到这一点,比如编写自己的代码来完成这项工作?

I've accomplished copying file through C filing but can't find a solution to delete a file through C.

我已经完成了通过 C 归档复制文件,但找不到通过 C 删除文件的解决方案。

采纳答案by urzeit

If you want to delete a file use the

如果要删除文件,请使用

remove

function. If you want to have a look behind the scenes of the standard library, you may download the source of the glibc (e.g.) and have a look at the implementation. You will see that actually a INTERNAL_SYSCALL will be performed on linux os:

功能。如果您想了解标准库的幕后情况,您可以下载 glibc 的源代码(例如)并查看其实现。您将看到实际上将在 linux os 上执行 INTERNAL_SYSCALL:

result = INTERNAL_SYSCALL (unlink, err, 1, file);

(from /sysdeps/unix/sysv/linux/unlinkat.c from the debian eglibc-2.15 package)

(来自 debian eglibc-2.15 包中的 /sysdeps/unix/sysv/linux/unlinkat.c)

If you want to go further and even not use that syscall you will have to implement your own file system logic since the file system syscall just gives an abstraction layer to different filesystems.

如果您想更进一步,甚至不使用该系统调用,您将必须实现自己的文件系统逻辑,因为文件系统系统调用只是为不同的文件系统提供了一个抽象层。

回答by jbr

The traditional way to delete a file is to use the unlink(2)function, which is called from remove(3), if path is a file.

删除文件的传统方法是使用unlink(2)函数,如果 path 是文件,则从remove(3)调用该函数。

回答by yoones

If you don't want to use the clean, usual way, you can open /dev/sd** and play with your file system.

如果你不想使用干净的、通常的方式,你可以打开 /dev/sd** 并使用你的文件系统。

Btw, remove() isn't a syscall (man 3 remove).

顺便说一句,remove() 不是系统调用(man 3 remove)。

回答by Dayal rai

int unlink (const char *filename)

The unlink function deletes the file name filename. The function unlink is declared in the header file unistd.h. This function returns 0 on successful completion, and -1 on error

unlink 函数删除文件名filename。函数 unlink 在头文件 unistd.h 中声明。此函数在成功完成时返回 0,在错误时返回 -1

回答by Sandeep_black

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/wait.h>


int main(){
        int status;
        pid_t pid = fork();
        if(-1 == pid){
                printf("fork() failed");
                exit(EXIT_FAILURE);
        }else if(pid == 0){
                execl("/bin/sh", "sh", "-c", "rm /tmp/san.txt", (char *) NULL);
        }else{
                printf("[%d]fork with id %d\n",pid);
                waitpid(pid,&status,0);
        }
return 0;
}