C语言怎么实现自我复制,求代码,加注释

2025-02-22 12:25:26
推荐回答(1个)
回答1:

#include "Windows.h"           //以下函数得包含此头文件
int main(){
char szPath[MAX_PATH];          //保存自身文件名的字符串
char toPath[MAX_PATH] = "C:\\test.exe";        //要复制到的路径字符串
int i;
GetModuleFileNameA(NULL, (LPCH)szPath, 225);     //获取自身路径的函数 

for (i = strlen(szPath); i > 0; i--)      //获取文件名的处理
{
if (szPath[i] == '\\')         //最后一个‘\’后面就是文件名
break;
}
//strcat(toPath, &szPath[i]);         //把文件名连接到toPath,得到“C:\*.exe”
for (char ch = 'c'; ch <= 'f'; ch++)      //复制处理,从c到f
{
toPath[0] = ch;                //改盘符   
CopyFileA(szPath, toPath, 1);       //复制函数,从szPath复制到toPath,强行覆盖原有文件
}
return 0;
}