I need to read the output from another program from my program. To do that, I can use popen(). This way, I get a FILE* to the program’s output that can be parsed in my program. How can I read and parse the content of the return value of popen() from my program? Alternatively, is there a more C++-like version of popen somewhere?
Share
You can do either create a stream buffer doing the moral equivalent of
popen()or you can create a stream buffer wrappingFILE*. Once you got a stream buffer, you can use it withstd::istreamwhich seems to be what you intend to do. A simple (and untested) stream buffer reading from aFILE*would look something like this:I reckon I mistyped something but roughly the idea does work: Just read from the
FILE*into a stream buffer. Alternatively, create a stream reading from a spawned process. This take a couple of platform specific calls tofork(),exec..(),pipe(),close(),dup2()(probably forgot something here, too) but can be done as well.A stream buffer like the one above could be used, e.g., like this: