2011-03-10 142 views
2

我想弄清楚如何將二進制文件從一個地方複製到另一個.exe's。我似乎無法找到任何解決方案來學習。複製二進制文件

我正在使用Windows。什麼是最好的方式來做到這一點?

+0

你應該張貼你已經嘗試過什麼,所以我們可以得到怎樣幫助一個想法 - 這是大多數編程語言中一個非常簡單的操作。 – Jeff 2011-03-10 17:14:12

回答

-2

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; 
} 
+3

Arrrgggg:聰明的報價!誰想到叫他們聰明?他們是DUMB引用...並且...'sizeof(char)',根據定義,''1' – pmg 2011-03-10 17:11:29

+0

你想要'n'什麼?你從不使用它。 – pmg 2011-03-10 17:14:02

+1

需要指定二進制模式(「rb」和「wb」),以便在區分文本和二進制「FILE」的系統(如Windows)上正常工作;如果文件不是BUFSIZE字節的確切倍數,那麼您正在編寫垃圾文件。 – 2011-03-10 18:50:16

2

確保您打開與O_BINARY選項中的文件,如果你使用open()和文件描述符,或用字母「B」的模式字符串,如果你使用fopen()。請注意,除Windows之外,O_BINARY不是標準的;但是,你可以在Unix上將它對待(或定義它)爲0,一切都會好的。如果你使用的是純粹的Windows API,那麼確保你正在做同樣的事情 - 確保你指定該文件被視爲二進制文件。

+0

'open'是一個POSIX函數,而Windows不是POSIX。 – 2011-03-10 18:02:30

6

你是什麼意思「最好的方式」?我覺得這是最簡單的方法......希望這是你的意思:)


fopen輸入和輸出文件的二進制模式

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); 
} 

freadfwrite

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"); 

最後關閉文件

if (fclose(exeout)) perror("close output file"); 
if (fclose(exein)) perror("close input file"); 

玩得開心!

1

Windows有一個CopyFile API(如果你不介意是平臺特定的)。有一點需要注意的是,確保您有權寫入目標區域。

0

您可以使用'dos.h'函數進行低級IO操作。以下代碼說明了它們的用法。希望這將有所幫助

#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); 
} 
-2
#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; 
} 
+0

破壞文件 – nithin 2016-03-06 16:10:30