我使用的代码是将一个可执行文件复制到另一个文件时,但是当我运行另一个文件时,Windows显示了此错误: “ Windows无法访问指定的设备,路径或文件。您可能没有适当的权限可以访问该项目。”
代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr1, *fptr2;
char filename[100];
int c;
printf("Enter file name to read: ");
scanf("%s", filename);
fptr1 = fopen(filename, "rb");
if (fptr1 == NULL)
{
printf("Cannot open file %s\n", filename);
exit(1);
}
printf("Enter file name to open for write: ");
scanf("%s", filename);
fptr2 = fopen(filename, "wb");
if (fptr2 == NULL)
{
printf("Cannot open file %s\n", filename);
exit(1);
}
while ((c = fgetc(fptr1)) != EOF)
{
fputc(c, fptr2);
}
fclose(fptr1);
fclose(fptr2);
return 0;
}
我缺少诸如清单或文件属性之类的东西?我不能简单地将可执行文件的字节复制到另一个可执行文件吗?这里实际上发生了什么,为什么我不能在我的情况下运行复制的可执行文件?
保存属性。
#include <windows.h>
#include <stdio.h>
int main(){
char src[100], dst[100];
printf("Source: ");
printf("Destination: ");
scanf("%s", dst);
if(!CopyFile(src, dst, FALSE)){
printf("Error: %lu\n", GetLastError());
return 1;
}
return 0;
}
#include <io.h>
#include <sys/stat.h>
_chmod(dst, _S_IREAD | _S_IWRITE | _S_IEXEC);