C/C++ 中的remove函数可用于删除文件。如果文件删除成功,该函数返回 0,其他返回非零值。
#include<stdio.h>
int main()
{
if (remove("abc.txt") == 0)
printf("Deleted successfully");
else
printf("Unable to delete the file");
return 0;
}
使用 C 中的 remove() 函数,我们可以编写一个程序,该程序在编译和执行后会自行销毁。
说明:这可以使用 C 中的remove 函数来完成。注意,这是在 Linux 环境中完成的。因此,remove 函数被提供给命令行参数中的第一个参数,即编译后创建的a.out文件(可执行文件)。因此程序将被销毁。
#include<stdio.h>
#include<stdlib.h>
int main(int c, char *argv[])
{
printf("By the time you will compile me I will be destroyed \n");
// array of pointers to command line arguments
remove(argv[0]);
// Note: argv[0] will contain the executable file i.e. 'a.out'
return 0;
}
输出:
By the time you will compile me I will be destroyed
在上面显示的输出之后,a.out文件将被删除。