cp命令是我们经常使用的Linux命令。
cp source destination :将source copy到destination。
自己利用fopen、fgets、fputs实现一个mycopy函数。
#if 0
自己实现一个cp source destination,将source的内容copy到destination
#endif
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
FILE *fps,*fpd;
int ch;
if(argc < 3){
printf("usage:%s <source_dir> <destination_dir>\n",argv[0]);
return -1;
}
fps = fopen(argv[1],"r");
if(fps == 0){
perror("fopen");
fclose(fps);
return -1;
}
fpd = fopen(argv[2],"w");
if(fpd == 0){
perror("fopen");
return -1;
}
while(1){
ch = fgetc(fps);
if(ch == EOF){
perror("fgets");
break;
}
fputc(ch,fpd);
}
fclose(fps);
fclose(fpd);
return 0;
}
diff a.c b.c :比较a.c和b.c两个文件中的内容是否一致。