- 论坛徽章:
- 95
|
原帖由 福瑞哈哥 于 2007-7-5 11:32 发表 ![]()
我是没见过。 if(ferror(stream)) {}里面写什么?
来个完整的例子吧,APUE2 的 Figure 15.11
- #include "apue.h"
- #include <sys/wait.h>
- #define PAGER "${PAGER:-more}" /* environment variable, or default */
- int
- main(int argc, char *argv[])
- {
- char line[MAXLINE];
- FILE *fpin, *fpout;
- if (argc != 2)
- err_quit("usage: a.out <pathname>");
- if ((fpin = fopen(argv[1], "r")) == NULL)
- err_sys("can't open %s", argv[1]);
- if ((fpout = popen(PAGER, "w")) == NULL)
- err_sys("popen error");
- /* copy argv[1] to pager */
- while (fgets(line, MAXLINE, fpin) != NULL) {
- if (fputs(line, fpout) == EOF)
- err_sys("fputs error to pipe");
- }
- if (ferror(fpin))
- err_sys("fgets error");
- if (pclose(fpout) == -1)
- err_sys("pclose error");
- exit(0);
- }
复制代码 |
|