如果我以这种方式在 Windows 中创建管道:
CreatePipe(hRead, hWrite, &sec_atr, NULL)
然后以这种方式从 hRead
生成 FILE *
:
int fd = _open_osfhandle((intptr_t)hRead, _O_RDONLY|_O_TEXT);
FILE *pipe_read = _fdopen(fd, "rt");
那我该如何关闭这个文件(pipe_read
)呢?
fclose(pipe_read)
_pclose(pipe_read)
CloseHandle((HANDLE)_get_osfhandle(fileno(pipe_read)))
最佳答案
来自示例here , 使用 fclose
。 fclose 的文档进一步阐明:
When these functions are used to close a stream, the underlying file descriptor and OS file handle (or socket) are closed, as well as the stream. Thus, if the file was originally opened as a file handle or file descriptor and is closed with fclose, do not also call _close to close the file descriptor; do not call the Win32 function CloseHandle to close the file handle; and do not call closesocket to also close the socket.
关于c++ - Windows中的管道句柄如何关闭? (_pclose() 的 fclose())?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5548273/