免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1050 | 回复: 2
打印 上一主题 下一主题

请教一个关于关闭流后其对应描述字的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-02-06 18:10 |只看该作者 |倒序浏览
请问是不是当描述字对应的流关闭时,其对应的描述字也同时关闭了,如下


  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <errno.h>

  5. void err_sys(const char *errmsg);

  6. int main(int argc, char **argv)
  7. {
  8.         FILE *fout;
  9.         char buff[] = "yinshulei\n";
  10.        
  11.         if ((fout = fdopen(STDOUT_FILENO, "w")) == NULL)
  12.                 err_sys("fdopen error");
  13.         else {
  14.                 if (fclose(fout) == EOF)
  15.                         err_sys("fclose error");
  16.                 if (write(STDOUT_FILENO, buff, strlen(buff)) != strlen(buff))
  17.                         err_sys("write error");
  18.         }

  19.         exit(0);
  20. }

  21. void err_sys(const char *errmsg)
  22. {
  23.         perror(errmsg);

  24.         exit(errno);
  25. }
复制代码


[root@localhost root]# gcc 2.c -o 2
[root@localhost root]# ./2
write error: Bad file descriptor
[root@localhost root]#

论坛徽章:
0
2 [报告]
发表于 2007-02-06 18:14 |只看该作者
--

当然。除非你 dup() 一下,在所得到的 fd 上用 fdopen()

--

论坛徽章:
0
3 [报告]
发表于 2007-02-06 18:18 |只看该作者
thank you very much!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP