So now I want to get the output of the command so I create another popen() in reading mode to get it.
No, creating an additional stream using popen(3)
won't work. However, since you are using FreeBSD you're in luck: on FreeBSD pipes are bidirectional, so popen(3)
accepts an r+
argument. From the manual:
Since
popen()
is now implemented using a bidirectional pipe, the type argument may request a bidirectional data flow.The type argument is a pointer to a null-terminated string which must be 'r' for reading, 'w' for writing, or 'r+' for reading and writing.
So you will probably get away with simply reading the output from the same FILE *
, with something like fgets(3)
.