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
廣告