0

我知道 popen 像文件一样打开管道以允许读/写,但是 fdopen 和 fputs/fgets 不是更有效吗?

4

2 回答 2

2

爆破

popen() 函数通过创建管道、分叉和​​调用 shell 来打开一个进程

fgets

fgets() 从流中最多读入一个小于 size 的字符,并将它们存储到 s 指向的缓冲区中。

popen打开管道,fgets读取数据。一个不会比另一个“更好”,因为他们做不同的事情。我想你可以说popen如果你想打开管道fgets更好,而如果你想读取数据更好,但这就是推动它。

于 2012-11-16T05:21:06.807 回答
2

The man page for popen: opens a process by creating a pipe, forking, and invoking the shell.

The man page for fopen: opens the file whose name is the string pointed to by path and associates a stream with it.

The man page for fgets: reads in at most one less than size characters from stream and stores them into the buffer pointed to by s.

popen is used to to open a pipe (usually to execute something like a shell command), and fopen is used to open a file that you can then use fgets to read from.

So popen and fgets are different functions that serve different purposes.

于 2012-11-16T05:22:55.013 回答