LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 730|回复: 1

这个程序出现了warning,但错误已修正

[复制链接]
发表于 2004-2-3 21:26:28 | 显示全部楼层 |阅读模式

  1. #include <sys/wait.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #define MAXLINE 4096                   /* max file length */
  7. #define DEF_PAGER        "/bin/more"         /* default pager program */
  8. int
  9. main(int argc,char **argv)
  10. {
  11.      int     n,fd[2];
  12.      pid_t   pid;
  13.      char    line[MAXLINE],*pager,*argv0;
  14.      FILE    *fp;
  15.      if(argc != 2) {
  16.               printf(" usage: a.out <PATHNAME> \n");
  17.               exit(0);
  18.      }
  19.      if((fp=fopen(argv[1],"r")) == NULL ) {
  20.               printf("fopen %s error\n",argv[1]);
  21.               exit(0);
  22.      }
  23.      if(pipe(fd)<0) {
  24.               printf("pipe error\n");
  25.               exit(0);
  26.      }
  27.      if((pid=fork())<0) {
  28.               printf("fork error\n");
  29.               exit(0);
  30.      }else if (pid>0) {                            /* parent */
  31.               close(fd[0]);                        /* close read end */
  32.                    /* parent copies argv[1] to pipe */
  33.               while(fgets(line,MAXLINE,fp) != NULL ) {
  34.                          n=strlen(line);
  35.                          if(write(fd[1],line,n) != n) {
  36.                                     printf("write error\n");
  37.                                     exit(0);
  38.                          }
  39.               }
  40.               if(ferror(fp)) {
  41.                          printf("fgets error\n");
  42.                          exit(0);
  43.               }
  44.               close(fd[1]);                     /* close write end of piep for reader */
  45.               if(waitpid(pid,NULL,0)<0) {
  46.                          printf("waitpid error\n");
  47.                          exit(0);
  48.               }
  49.               exit(0);
  50.      }else{                                            /* child */
  51.               close(fd[1]);                     /* close write end */
  52.               if(fd[0] != STDIN_FILENO) {
  53.                          if(dup2(fd[0],STDIN_FILENO) != STDIN_FILENO) {
  54.                                     printf("dup2 error to stdin\n");
  55.                                     exit(0);
  56.                          }
  57.                          close(fd[0]);
  58.               }
  59.                          /* get arguments for execl() */
  60.               if( ( pager = getenv ( "PAGER" )) == NULL )
  61.                          pager=DEF_PAGER;
  62.               if((argv0=strrchr(pager,'/')) != NULL )
  63.                          argv0++;              /* step past rightmost slash */
  64.               else
  65.                          argv0=pager;          /* no slash in pager */
  66.               if(execl(pager,argv0,(char *) 0) <0) {
  67.                          printf("execl() error for %s\n",pager);
  68.               }
  69.      }
  70.      exit(0);
  71. }

  72. 14.2.c: In function `main':
  73. 14.2.c:40: warning: passing arg 1 of `ferror' from incompatible pointer type
  74. 14.2.c:60: warning: assignment makes pointer from integer without a cast
复制代码


上面的程序错误已改正。程序下面的WARNING将不存在。
现在懂了在40行错了是fp 不是fd ferror参数的类型是FILE * ,不是fd,
发表于 2004-2-4 00:07:39 | 显示全部楼层
印刷错误。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表