你是什么意思"最好的方式"?我认为这是最直接的方式......希望这就是你的意思:)
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)
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");
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}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
13119 次 |
| 最近记录: |