复制二进制文件

问题描述 投票:6回答:7

我试图弄清楚如何将二进制文件从一个位置复制到另一个.exe。我似乎找不到要学习的解决方案。

我正在使用Windows。最好的方法是什么?

c
7个回答
7
投票

您是什么意思“最好的方式”?我认为这是最直接的方式……希望这就是您的意思:)


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

玩得开心!


2
投票

如果使用open()和文件描述符,请确保使用O_BINARY选项打开文件;如果使用fopen(),请在模式字符串中使用字母“ b”打开文件。请注意,除Windows之外,O_BINARY不是标准的;但是,您可以在Unix上将其(或定义为0)对待,一切都会好的。如果您使用的是纯Windows API,请确保您正在执行等效操作-确保您将文件指定为二进制文件。


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

0
投票

您可以使用它来复制二进制文件

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

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

-3
投票

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.