我有一些遗留代码可以到处执行此操作:
int fd; // open a file
if(fd == -1)
{
close(fd);
}
我觉得这很不对。
关闭无效的文件描述符是否有效?
最佳答案
根据手册:
Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned and the global integer variable errno is set to indicate the error.
然后:
The close() system call will fail if: [EBADF] fildes is not a valid, active file descriptor.
那么就不会发生任何有害的事情了。
关于c - 关闭无效的文件描述符有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40915329/