C语言实现linux命令cat

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main(int argc, char *argv[])
 5 {
 6     if(argc == 2)//将文件中的打印到标准输出
 7     {
 8         FILE *fp;
 9         if( !(fp = fopen( argv[1], "r" )) )
10         {
11             fprintf(stderr,"cannot open file %s.",argv[1]);
12             exit(-1);
13         }
14         int c;
15         while( (c = fgetc(fp) ) != EOF )
16         {
17             fputc(c,stdout);
18         }
19         fclose(fp);
20     }
21     else if( argc == 3)
22     {
23         FILE *src ;
24         FILE *dest;
25         if( !( src = fopen( argv[1], "r" ) ) )
26         {
27             fprintf(stderr, "%s: cannot open file %s\n", argv[0],argv[1]);
28             exit(-1);
29         }
30         if( !( dest = fopen( argv[2], "w+" ) ) )
31         {
32             fprintf(stderr, "%s: cannot open file %s\n", argv[0],argv[1]);
33             exit(-1);
34         }
35         int c;
36         while( (c = fgetc(src))!=EOF)
37         {
38             fputc(c,dest);
39         }
40         fclose(src);
41         fclose(dest);
42     }
43     return 0;
44 }

 

posted @ 2015-02-28 19:22  永久指针  阅读(2321)  评论(0编辑  收藏  举报