如何在unix管道句柄怎么样? (pclose()的fclose())? [英] How close pipe handle in unix? (fclose() of pclose())?
问题描述
如果我在unix中以这种方式创建pipe:
If I create pipe in unix this way:
int fds[] = {0, 0};
pipe(fds);
然后从<$ c>中创建 FILE *
$ c> fds [0] 这样:
Then make FILE *
from fds[0]
this way:
FILE *pipe_read = fdopen(fds[0], "rt");
然后我应该怎么关闭这个文件( pipe_read
)?
Then how should I close this file (pipe_read
)?
-
fclose(pipe_read)
-
pclose(pipe_read)
-
close(fileno(pipe_read))
fclose(pipe_read)
pclose(pipe_read)
close(fileno(pipe_read))
推荐答案
fdopen
FILE *
所以你应该 fclose
它。这也将关闭底层文件描述符。
fdopen
returns a FILE*
so you should fclose
it. This will also close the underlying file descriptor as well.
pclose
调用意味着关闭一个 popen
,一个用于运行命令并用管道连接的命令。
The pclose
call is meant for closing a handle created with popen
, a function you use for running a command and connecting to it with pipes.
close
调用将关闭底层文件描述符,但不幸的是,在文件句柄有机会刷新其数据之前 - 换句话说,你可能会丢失数据。
The close
call will close the underlying file descriptor but, unfortunately, before the file handle has had a chance to flush its data out - in other words, you're likely to lose data.
这篇关于如何在unix管道句柄怎么样? (pclose()的fclose())?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!