日期:2014-05-16  浏览次数:20637 次

----------简单问题求解答--------
dup2(fd, 0);
scanf("%s", buffer);

为啥scanf语句直接跳过了。


dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);

因为0,1,2都是shell用的,这样不会有异常?


感觉对文件描述符没有理解透彻。

------解决方案--------------------
int dup2(int oldfd, int newfd);
dup2() makes newfd be the copy of oldfd, closing newfd first if necessary, but note the following:

       *  If oldfd is not a valid file descriptor, then the call fails, and newfd is not closed.

       *  If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then dup2() does nothing, and returns newfd.
看了这个你就知道了,就是你把标准输入关闭了,然后用fd代替,所以没有忽略你的scanf只是scanf立刻从fd里面取了点东西出来。