c - POSIX 系统上的 `fileno(stdout) != 1` 可能吗?

标签 c linux bsd

假设STDOUT_FILENOstdout 文件描述符可以不同于1 ( stdout need not be a modifiable lvalue ) ?

例如,可以freopen("/dev/null", "w", stdout)更改 fileno(stdout) 结果?

最佳答案

是的。

测试程序:

#include <stdio.h>

int main() {
    fclose(stdin);
    freopen("stdout.txt", "w+", stdout);
    fprintf(stderr, "%d\n", fileno(stdout));
    return 0;
}

这会在我的机器 (OS X 10.9.4) 上打印 0。

文件描述符通常从最低编号开始重用。通过关闭stdin,文件描述符0被释放,后续的freopen打开文件时将使用文件描述符0。

关于c - POSIX 系统上的 `fileno(stdout) != 1` 可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25516375/

相关文章:

c - 对如何使用 malloc()/calloc() 创建内存的误解

c - "Linking Error: Duplicate public_main in module"构建时

我可以返回在函数中创建的缓冲区吗?

c - 我应该包括手册页概要中列出的任何或所有标题吗?

c - FRDM-KL46Z 内部引用时钟为 48 MHz?

linux - 将日期添加到 shell 脚本中的文件名

linux - 动态创建 proc 目录条目?

linux - 输入-PSSession 返回 PSInvalidOperationException

linux - 开发没有信息的驱动程序

linux - bsd rc.d 的重生选项