How to check if a given file descriptor stored in a variable is still valid?

前端 未结 6 1004
孤独总比滥情好
孤独总比滥情好 2020-11-29 01:17

I have a file descriptor stored in a variable say var. How can I check whether that descriptor is valid at a later stage?

  fdvar1= open(.....);
  fdvar2 =          


        
6条回答
  •  借酒劲吻你
    2020-11-29 01:33

    You can use the fcntl() function:

    int fd_is_valid(int fd)
    {
        return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
    }
    

提交回复
热议问题