Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying Binary Files

Tags:

c

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?

like image 221
Jack Harvin Avatar asked Mar 10 '11 16:03

Jack Harvin


People also ask

What does binary copy mean?

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.

How do I copy a binary file in Python?

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.


1 Answers

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!

like image 60
pmg Avatar answered Oct 10 '22 06:10

pmg