<unistd.h>的close(0)导致std::cout被关闭

代码如下:其中ZJ::open_max返回系统允许的打开的文件的最大个数

#include "util.h"

#include <unistd.h> // int close(int fd);
#include <iostream>

int main(void)
{
    const long opmax = ZJ::open_max();
    std::cout << "opmax = " << opmax << std::endl;
    for (int i = 0; i < opmax; ++i) {
        std::cout << "close file " << i << ": " << close(i) << std::endl;
    }
    return 0;
}

假设返回的opmax值是1024

但运行结果是:

opmax = 1024
close file 0: 0

为什么?

大哥,close(0)就把cout给关闭了啊!

posted @ 2015-04-29 00:40  rldts  阅读(665)  评论(0编辑  收藏  举报