c - 为什么 C 程序可以删除自身然后运行另一个任务?

标签 c

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("remove %s\n", argv[0]);
    remove(argv[0]);

    printf("rename %s to %s\n", argv[1], argv[2]);
    rename(argv[1], argv[2]);
}


$g++ hello.cpp -o hello
$touch tmp
$./hello tmp temp
remove ./hello
rename tmp to temp

为什么可能?

最佳答案

至少在 Unix 中(包括 Linux 和 Mac OS X),如果你在有人打开文件时删除文件,我实际上不会消失,直到打开它的程序关闭文件或终止。

关于c - 为什么 C 程序可以删除自身然后运行另一个任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5480866/

相关文章:

c++ - Python CTYPE:如何释放内存?获取无效的指针错误

c - 什么时候应该使用 struct node* head=NULL 和 struct node *head=NULL ?

python - 将Python转换为C,然后用Cython编译成exe

c - 使用 C 的指针应用程序

c - GetThemeStream 用法

c - 为什么 scanf 会导致我的代码无限期运行?

c - 附加到字符串的未打印字符

c - 我在编译代码时遇到错误。 '>=' 标记之前的预期表达式

c - 从文件读取时重复字符串但不重复整数

字符比较无法结束循环