为什么打开使我的文件描述符为0? [英] Why does open make my file descriptor 0?

查看:207
本文介绍了为什么打开使我的文件描述符为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用管道和叉子的程序,需要将写端更改为输出文件.但是,当我打开文件时,文件描述符为0,通常为stdin,但我认为这是造成我某些问题的原因.这是我的代码

I'm working on a program that is using a pipe and forks and need to change the write end to an output file. But when I open a file the file descriptor is 0 which is usually stdin but which I think is the cause of some of my problems. Here is my code

if (outputfd = open("file", O_RDWR | O_CREAT | O_TRUNC) == -1) 
{
    // open failed
}

有人可以让我知道为什么是0吗?或如何解决?

Can someone let me know why it is 0? Or how to fix it?

推荐答案

这是因为您正在将其与-1进行比较.

It's because you're comparing it to -1.

outputfd没有得到open的结果.它获取-1的检查结果.

outputfd doesn't get the result of open. It gets the result of the check for -1.

这篇关于为什么打开使我的文件描述符为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆