#include <windows.h>
#include <stdio.h>
#define COPY_LOCATION "C:\\copy.exe"
void copyFile()
{
char location[MAX_PATH];
GetModuleFileName(NULL, location, MAX_PATH); // Gets the full location of the currently running process
puts(location); // Print it out
FILE * original;
original = fopen(location, "rb"); // Open the file for reading binary
if(original == NULL) // Error opening
{
fprintf(stderr, "Error opening file [%s]\r\n", GetLastError());
getchar();
ExitProcess(0);
}
fseek(original, 0, SEEK_END); // Seek to the end of the file
long size = ftell(original); // Get our file size
rewind(original); // Go back to the start of the file
char bytes[size];
int result = fread (bytes,1,size,original); // Read our bytes from this file
if(result != size) // Didn't get it all
{
fprintf(stderr, "Error reading file [%s]\r\n", GetLastError());
fclose(original);
getchar();
ExitProcess(0);
}
FILE * copied;
copied = fopen(COPY_LOCATION, "wb"); // Open to write-binary
int wrote = fwrite(bytes, 1, size, copied);
if(wrote != size) // Didn't write it all
{
fprintf(stderr, "Error copying file [%s]\r\n", GetLastError());
fclose(original);
getchar();
ExitProcess(0);
}
fclose(copied); // Close copy
fclose(original); // Close original
puts("Copy complete!");
getchar();
}
int main(int argc, char **argv)
{
copyFile();
}