c语言: 文件io, 拷贝文件(二进制)

时间:2022-06-01 20:35:19
#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();
}
if ((fout=fopen(to,"wb"))==NULL) {
perror("fopen fileout");
exit();
} while(!feof(fin)) {
size = fread(buf, , TRAN_SZIE, fin);
fwrite(buf, , size, fout);
} fclose(fin);
fclose(fout);

return ;
} int main(int argc, char * argv[])
{
int ret = ;
if (argc!=) {
printf("Usage: %s filein fileout\n", argv[]);
exit();
} ret = copy_bin(argv[], argv[]); return ret;
} /*
[root@localhost]# ./copy_bin IMG1.jpg IMG2.jpg
[root@localhost]# diff IMG1.jpg IMG2.jpg
*/