想象下面的代码作为线程运行:
void *thread_worker(void *q) {
for (;;) {
int fd = some_queue_get(q);
FILE *writer = fdopen(fd, "w");
if (!writer) { perror("fdopen"; close(fd); continue; }
// do something with writer
if (fclose(writer) == EOF) {
perror("fclose writer");
// should fd be closed here?
close(fd);
}
}
fclose(3)
可能由于各种原因而失败-是否可以保证/当底层文件描述符关闭时或之后仍然打开?最佳答案
man fclose
也不提供我系统上的答案,但是man 3p fclose
揭示了official version from the POSIX Programmer's manual,它在此问题上更加全面:
The
fclose()
function shall perform the equivalent of aclose()
on the file descriptor that is associated with the stream pointed to by stream.
关于c - 如果fclose()失败,文件描述符是否仍然打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66048264/