#include<stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
if (argc < 2)
{
printf("请在命令行输入文件名\n");
return -1;
}
//以读的方式打开源文件
FILE* fp = fopen(argv[1],"r");
if (NULL==fp)
{
perror("fopen");
return -1;
}
//以写的方式打开目录文件
FILE* fp_w = fopen("copy.c" ,"w+");
if (NULL==fp_w)
{
perror("fopen");
return -1;
}
//复制拷贝
char str[128] = "";
while(fgets(str, sizeof(str), fp) != NULL)
{
fputs(str, fp_w);
}
//关闭文件
fclose(fp);
fclose(fp_w);
return 0;
}
ubuntu@ubuntu:io$ ./a.out
请在命令行输入文件名
ubuntu@ubuntu:io$ ./a.out 13test.c
ubuntu@ubuntu:io$ diff 13test.c copy.c
ubuntu@ubuntu:io$