C语言中的自毁代码
在这里,我们将看到如何在C语言中创建自毁代码。自毁代码基本上是在执行该代码,然后在执行后删除可执行文件。
这个任务很简单。我们需要获取可执行文件名才能将其删除。我们可以使用命令行参数。argv[0]将保存可执行文件名。然后使用remove()方法我们可以将其删除。
在程序中,我们可以看到删除该文件后正在打印一行。因此,现在的问题来了,当前文件不存在时下一行如何执行?
实际上,整个转换后的代码在执行之前都会复制到主存储器中。执行文件的内容被复制;它本身不使用。因此,将从主存储器中打印下一行。
示例
#include<stdio.h>
int main(int c, char *argv[]) {
printf("After completing this, the file will be removed\n");
remove(argv[0]); //remove the argv[0] this is the name of the executable
printf("Removed\n");
return 0;
}输出结果
After completing this, the file will be removed Removed