我知道 popen 像文件一样打开管道以允许读/写,但是 fdopen 和 fputs/fgets 不是更有效吗?
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.