复制二进制文件

Jac*_*vin 6 c

我试图找出如何将二进制文件从一个地方复制到另一个地方.exe的.我似乎找不到任何可以学习的解决方案.

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

pmg*_*pmg 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);
}
Run Code Online (Sandbox Code Playgroud)

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");
Run Code Online (Sandbox Code Playgroud)

最后关闭文件

if (fclose(exeout)) perror("close output file");
if (fclose(exein)) perror("close input file");
Run Code Online (Sandbox Code Playgroud)

玩得开心!


The*_*GiG -4

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

\n\n
#include <ctype.h>\n#include <stdio.h>\n#define BUFSIZE 1024\n\nint main(int argc, char **argv)\n{\ncharmybuf[BUFSIZE] = { 0 }, *p = NULL;\nFILE *ifd = NULL, *ofd = NULL;\nifp = fopen( argv[1], \xe2\x80\x9cr\xe2\x80\x9d );\nofp = fopen( argv[2], \xe2\x80\x9cw\xe2\x80\x9d );\nassert(ifp!=NULL);\nassert(ofp!=NULL);\nwhile ( ( n = fread( mybuf, sizeof(char), BUFSIZE ,ifd) ) > 0 )\n{\n    fwrite(mybuf, sizeof(char),BUFSIZE,ofd);\n}\nfclose(ifd);\nfclose(ofd);\nreturn 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

  • Arrrgggg:聪明的报价!谁想到称他们聪明???它们是愚蠢的引号...并且...`sizeof(char)`,根据定义,是`1` (3认同)