Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209322
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: C/C++

2011-03-27 14:22:21

  1. /* k&r(7.5): File Access, Error Handling
  2.    created on mar 27, 2011
  3.    */

  4. #include "stdio.h"

  5. /* cat: concatenate files, version 2 */
  6. main(int argc, char *argv[])
  7. {
  8.     FILE *fp;
  9.     void filecopy(FILE *, FILE *);
  10.     char *prog = argv[0]; /* program name for errors */

  11.     if (argc == 1) /* no args; copy standard input */
  12.      filecopy(stdin, stdout);
  13.     else
  14.      while (--argc > 0)
  15.         if ((fp = fopen(*++argv, "r")) == NULL) {
  16.             fprintf (stderr, "%s: can't open %s\n", prog, *argv);
  17.             exit(1);
  18.         } else {
  19.             filecopy(fp, stdout);
  20.             fclose(fp);
  21.         }
  22.     if (ferror(stdout)) {
  23.         fprintf(stderr, "%s: error writing stdout\n", prog);
  24.         exit(2);
  25.     }
  26.     exit(0);
  27. }

  28. /* filecopy: copy file ifp to file ofp */
  29. void filecopy(FILE *ifp, FILE *ofp)
  30. {
  31.     int c;

  32.     while ((c = getc(ifp)) != EOF)
  33.      putc(c, ofp);
  34. }
阅读(416) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~