C 复制二进制文件

C 复制二进制文件,c,C,我想知道如何将二进制文件从一个地方复制到另一个.exe。我似乎找不到任何学习的方法 我正在使用Windows。最好的方法是什么? #包括 #包括 #定义bufsize1024 int main(int argc,字符**argv) { charmybuf[BUFSIZE]={0},*p=NULL; 文件*ifd=NULL,*ofd=NULL; ifp=fopen(argv[1],“r”); ofp=fopen(argv[2],“w”); 断言(ifp!=NULL); 断言(ofp!=NULL);

我想知道如何将二进制文件从一个地方复制到另一个.exe。我似乎找不到任何学习的方法

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

#包括
#包括
#定义bufsize1024
int main(int argc,字符**argv)
{
charmybuf[BUFSIZE]={0},*p=NULL;
文件*ifd=NULL,*ofd=NULL;
ifp=fopen(argv[1],“r”);
ofp=fopen(argv[2],“w”);
断言(ifp!=NULL);
断言(ofp!=NULL);
而((n=fread(mybuf,sizeof(char),BUFSIZE,ifd))>0)
{
fwrite(mybuf、sizeof(char)、BUFSIZE、ofd);
}
fclose(ifd);
fclose(ofd);
返回0;
}

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

什么是“最佳方式”?我认为这是最直接的方式。。。希望这就是你的意思:)


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);
}
fread
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");
最后关闭文件

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

玩得开心

Windows有一个平台(如果您不介意特定于平台的话)。现在需要注意的一件事是确保您有写入目标区域的权限。

您可以使用“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);
}
#包括
#包括
#包括
#包括
void main()
{
char s_文件[100],d_文件[100],buf[512];
短字符拷贝='y';
int in_句柄、out_句柄、flg、len;
printf(“\n输入文件名:”);
fflush(stdin);
获取(s_文件);
printf(“\n输入目标文件名:”);
fflush(stdin);
获取(d_文件);
//打开文件进行读取
in_handle=open(s_文件,O_RDONLY | O_二进制文件);
if(in_handle0)
{
flg=写入(输出手柄、buf、len);
如果(flg==-1)
打破
}
如果(flg==-1)
{
printf(“无法创建”);
}
其他的
{
printf(“创建的文件”);
}
}
如果(!(在手柄
中)包括
#包括
int main()
{
文件*fp1,*fp2;
字符c;
fp1=fopen(“源文件”、“rb”);
如果(fp1==NULL)
出口(1);
fp2=fopen(“目标文件”、“wb”);
如果(fp2==NULL)
出口(1);
而((c=fgetc(fp1))!=EOF)
fputc(c,fp2);
fclose(fp1);
fclose(fp2);
返回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);
}

ARRRGGG:smart quotes!!!谁想叫它们smart???它们是哑引号…而且…
sizeof(char)
,顾名思义,是
1
你想要什么
n
?你从来没有用过它做任何事。需要指定二进制模式(“rb”和“wb”)才能在系统(如Windows)上正常工作区分文本和二进制“文件”,如果文件不是BUFSIZE字节的精确倍数,那么您就是在写垃圾。您应该发布您尝试过的任何内容,这样我们就可以了解如何提供帮助-在大多数编程语言中,这是一个非常简单的操作。
open
是一个POSIX函数,Windows不是POSIX。
#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;
}
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);
}