7

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?

Jack Harvin
  • 6,605
  • 7
  • 23
  • 21
  • You should post anything you have tried, so we can get an idea of how to help - this is a pretty easy operation in most programming languages. – Jeff Mar 10 '11 at 17:14

7 Answers7

13

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!

pmg
  • 106,608
  • 13
  • 126
  • 198
2

Make sure you open the files with the O_BINARY option if you use open() and file descriptors, or with the letter "b" in the mode string if you use fopen(). Note that O_BINARY is not standard except on Windows; however, you can treat it (or define it) as 0 on Unix and all will be fine. If you are using a pure Windows API, then make sure you are doing the equivalent - make sure you specify that the file is treated as a binary file.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2

Windows has a CopyFile API (if you don't mind being platform specific). One thing to be careful of these days is making sure you have permissions to write to the destination area.

Jeff
  • 1,969
  • 3
  • 21
  • 35
0

You can use this to copy Binary Files:

int get_file_size(char *source)
{
    FILE *fichier = fopen(source,"rb");
    fseek(fichier,0,SEEK_END);
    int size = ftell(fichier);
    fseek(fichier,0,SEEK_SET);
    fclose(fichier);
    return size;
}
void copy(char *source, char *dest)
{
    int srcsize = get_file_size(source);
    char *data = (char *)malloc(srcsize);
    int fsource = open(source,O_RDONLY | O_BINARY);
    int fdest = open(dest,O_WRONLY | O_CREAT | O_BINARY);
    read(fsource,data,srcsize);
    write(fdest,data,srcsize);
    close(fsource);
    close(fdest);
}
Jaysmito Mukherjee
  • 1,467
  • 2
  • 10
  • 29
Calf
  • 9
  • 1
  • Many problems: Why split the code in 2 separate functions that use different APIs? Type `int` is inappropriate for a file size, (although in Microsoft Windows, `int` and `long` have the same size...) No test for `fopen()` or `open` failure. No test for `malloc()` failure. Missing 3rd argument to `open` with `O_CREAT` flag. No test on `read` nor `write`. No useful return value... – chqrlie Aug 18 '21 at 10:20
-1

You can use function of 'dos.h' for low-level IO operation. Following code illustrate use of them. Hope it will helpful

#include<stdio.h>
#include<dos.h>
#include<FCNTL.H>
#include<SYS\STAT.H>
void main()
{
    char s_file[100],d_file[100],buf[512];
    short char copy='y';
    int in_handle,out_handle,flg,len;
    printf("\nEnter File Name : ");
    fflush(stdin);
    gets(s_file);
    printf("\nEnter Destination File Name : ");
    fflush(stdin);
    gets(d_file);
    // open file for reading
    in_handle=open(s_file,O_RDONLY | O_BINARY);
    if(in_handle<0)
    {
        printf("\nSource File not Found... ");
    }
    else
    {
        // open file for writing
        out_handle=open(d_file,O_CREAT|O_WRONLY|O_BINARY,S_IWRITE);
        while((len=read(in_handle,buf,512))>0)
        {
            flg=write(out_handle,buf,len);
            if(flg==-1)
                break;
        }
        if(flg==-1)
        {
            printf("Unable to Create");
        }
        else
        {
            printf("File Created");
        }
    }
     if(!(in_handle<0))
        close(in_handle);
     if(!(out_handle<0));
        close(out_handle);
}
Mahesh Meniya
  • 2,627
  • 3
  • 18
  • 17
  • `S_IWRITE` is incorrect. you should also specify read and execute permissions. – chqrlie Aug 18 '21 at 10:24
  • DV because of `void main()`, `fflush(stdin);`, `gets(s_file);`, `gets(d_file);`. Furthermore `write(out_handle,buf,len);` could return a short count, indicating only a portion of `buf` was written. You only test for write error. – chqrlie Aug 18 '21 at 10:27
-3
#include<stdio.h>
#include<stdlib.h>
int main()
{
     FILE *fp1,*fp2;
     char c;
     fp1=fopen("source file","rb");
     if(fp1==NULL)
       exit(1);
     fp2=fopen("destination file","wb");
     if(fp2==NULL)
       exit(1);
     while((c=fgetc(fp1))!=EOF)
          fputc(c,fp2);
     fclose(fp1);
     fclose(fp2);
     return 0;
}
p2013
  • 438
  • 3
  • 6
  • 12
  • While this might not be the *best* solution, it would at least be a correct solution if `c` was defined with type `int`. – chqrlie Aug 18 '21 at 10:29
-4

http://www.cs.bu.edu/teaching/c/file-io/intro/

#include <ctype.h>
#include <stdio.h>
#define BUFSIZE 1024

int main(int argc, char **argv)
{
charmybuf[BUFSIZE] = { 0 }, *p = NULL;
FILE *ifd = NULL, *ofd = NULL;
ifp = fopen( argv[1], “r” );
ofp = fopen( argv[2], “w” );
assert(ifp!=NULL);
assert(ofp!=NULL);
while ( ( n = fread( mybuf, sizeof(char), BUFSIZE ,ifd) ) > 0 )
{
    fwrite(mybuf, sizeof(char),BUFSIZE,ofd);
}
fclose(ifd);
fclose(ofd);
return 0;
}
The GiG
  • 2,571
  • 2
  • 28
  • 29
  • 3
    Arrrgggg: smart quotes !!! whoever thought of calling them smart??? they're DUMB QUOTES ... and ... `sizeof(char)`, by definition, is `1` – pmg Mar 10 '11 at 17:11
  • What do you want `n` for? You never use it for anything. – pmg Mar 10 '11 at 17:14
  • 1
    Need to specify binary mode ("rb" and "wb") for this to work correctly on systems (like Windows) that distinguish between text & binary "FILE"s; and you're writing garbage if the file is not an exact multiple of BUFSIZE bytes. – David Gelhar Mar 10 '11 at 18:50