c语言: 文件io, 拷贝文件(二进制)
#include <stdio.h> #include <stdlib.h>
#define TRAN_SZIE 1024 int copy_bin(char* from, char* to) { FILE *fin, *fout; int size; char buf[TRAN_SZIE];
if ((fin=fopen(from,"rb"))==NULL) { perror("fopen filein"); exit(0); }
if ((fout=fopen(to,"wb"))==NULL) { perror("fopen fileout"); exit(0); } while(!feof(fin)) { size = fread(buf, 1, TRAN_SZIE, fin); fwrite(buf, 1, size, fout); } fclose(fin); fclose(fout);
return 0; } int main(int argc, char * argv[]) { int ret = 0; if (argc!=3) { printf("Usage: %s filein fileout\n", argv[0]); exit(0); } ret = copy_bin(argv[1], argv[2]); return ret; } /* [root@localhost]# ./copy_bin IMG1.jpg IMG2.jpg [root@localhost]# diff IMG1.jpg IMG2.jpg */