代码改变世界

求Linux C++高手解答

2011-08-04 19:57  Aga.J  阅读(652)  评论(0编辑  收藏  举报

问题如下,今天整天就耗在和它有关的地方上了……

#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<fcntl.h>

void main()
{
  char *buf="hello\n";
  int len=strlen(buf);
  close(1);
  FILE* fd0=freopen("/dev/tty","w",stdout);
  write(1,buf,len+1);
  //如果close(1),然后没有调用freopen,然后直接write就会发生bad file descriptor的错误,结论正确!
  //如果没有调用close(1),也没有调用freopen,然后直接write,则会成功输出到shell,结论正确!
  //如果调用close(1)后再调用reopen,然后调用write,则会发生bad file descripto的错误,但是shell会显示所写的内容,神马意思??? 我要问的就是这里

  FILE *fd=fopen("tmp.log","r+");
  if(fd!=NULL)
   fputs(strerror(errno),fd);
  fclose(fd);
}

谢谢!

被移出首页,这个问题看来只能自己解了