#include<stdio.h>
int main(int argc, char *argv[]){
if (argc != 3)
{
printf("usage : ./copy filename1 file name2\n");
}
char buffer[1024] = {0};
FILE *file1 = fopen(argv[1], "r"); //argv为路径名
if (NULL == file1)
{
perror("open1");
return 1;
}
FILE *file2 = fopen(argv[2], "w");
if (NULL == file2)
{
perror("open2");
return 2;
}
int count;
while (count = fread(buffer, sizeof(char), 1024, file1)) //循环读取文件,返回值为字符长度
{
if (0 == count)
{
perror("read");
fclose(file1);
fclose(file2);
return 3;
}
int count2 = fwrite(buffer, sizeof(char), count, file2); //写入新文件
if (0 == count2)
{
perror("write");
fclose(file1);
fclose(file2);
return 4;
}
}
fclose(file1);
fclose(file2);
}