I'm trying to figure out how to copy binary files from one place to another .exe's. I can't seem to find any solutions to learn.
I'm using Windows. What's the best way to do it?
A binary copy faithfully reproduces every single one of those 1s and 0s, regardless of what they signify. A binary copy will be 100% identical to the source. This means it is possible to check the copy against the source, and verify that it is a 100% successful copy.
Calling shutil. copy(source, destination) will copy the file at the path source to the folder at the path destination. (Both source and destination are strings.) If destination is a filename, it will be used as the new name of the copied file.
What do you mean "best way"? I think this is the most straightforward way ... hopefully that is what you meant :)
fopen
the input and output files with binary mode
FILE *exein, *exeout;
exein = fopen("filein.exe", "rb");
if (exein == NULL) {
/* handle error */
perror("file open for reading");
exit(EXIT_FAILURE);
}
exeout = fopen("fileout.exe", "wb");
if (exeout == NULL) {
/* handle error */
perror("file open for writing");
exit(EXIT_FAILURE);
}
fread
and fwrite
size_t n, m;
unsigned char buff[8192];
do {
n = fread(buff, 1, sizeof buff, exein);
if (n) m = fwrite(buff, 1, n, exeout);
else m = 0;
} while ((n > 0) && (n == m));
if (m) perror("copy");
and finally close the files
if (fclose(exeout)) perror("close output file");
if (fclose(exein)) perror("close input file");
Have fun!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With