mycat实现linux cat命令相似功能
/*
* mycat.c
*
* Copyright 2013 spark <spark@spark-vbox>
*
*/
#include <stdio.h>
int main(int argc, char **argv)
{
FILE *fp;
void filecopy(FILE *, FILE *);
if(argc == 1){
filecopy(stdin,stdout);
}
else{
while(--argc>0){
if( (fp=fopen(*++argv,"r")) ==NULL){
printf("fail to open file %s.\n",*argv);
return 1;
}
else{
filecopy(fp,stdout);
fclose(fp);
}
}
}
return 0;
}
void filecopy(FILE * from, FILE * to){
int ch;
while((ch=getc(from))!=EOF){
putc(ch,to);
}
}
以上来自《C Programming Language》
总以为linux命令的实现较神秘微妙,以上实现和cat命令相似功能。感觉很简洁很美啊~!

浙公网安备 33010602011771号