Search code examples
cfile-descriptor

Why does open make my file descriptor 0?


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
}

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


Solution

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

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