Reputation: 6755
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?
Upvotes: 7
Views: 26843
Reputation: 9
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);
}
Upvotes: 0
Reputation: 432
#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;
}
Upvotes: -3
Reputation: 2605
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);
}
Upvotes: -1
Reputation: 753970
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.
Upvotes: 2
Reputation: 2611
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;
}
Upvotes: -4
Reputation: 108938
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!
Upvotes: 13
Reputation: 2009
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.
Upvotes: 2