Question

I'm using FreeBSD and C/C++ to control the robot. Here is the issue:

I use popen() in writing mode to give the command to computer in order to control the robot. So now I want to get the output of the command so I create another popen() in reading mode to get it.

Will it work?

Should I create some kind of parent and child process?

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top