使用二进制打开可以复制大型文件如:.exe文件,音频视频文件等。
#include <stdio.h>
#define MAXSIZE 4096
#define MAXPATH 1024
int main(int argc, char *argv[])
{
if (argc != 3)
{
fprintf(stdout, "Usage: mycp sourcefile targetfile!\n");
return -1;
}
if (strlen(argv[1]) > MAXPATH || strlen(argv[2] > MAXPATH))
{
fprintf(stdout, "FilePath is too long to copy!\n");
return -2;
}
char sourcefilename[MAXPATH] = { 0 };
char targetfilename[MAXPATH] = { 0 };
char tmpBuf[4096] = { 0 };
int n = 0;
strcpy(sourcefilename, argv[1]);
strcpy(targetfilename, argv[2]);
FILE *fp1 = fopen(sourcefilename, "rb");
FILE *fp2 = fopen(targetfilename, "wb");
if (fp1 == NULL || fp2 == NULL)
{
printf("open a file error!\n");
}
int count = 0;
while (/*!feof(fp1)*/(count = fread(tmpBuf, sizeof(char), MAXSIZE, fp1)))
{
//每次最多读4096个字节的内容
fwrite(tmpBuf, sizeof(char), count, fp2);//把每次实际读到的数据写入目标文件
}
fclose(fp1);
fclose(fp2);
return 0;
getchar();
}