Question

Here is my code:

#include<stdlib.h>
#include<stdio.h>

int main()
{
(void)system("cat json.json | ./jq.exe '.location | {street, city}'");
return 0;
}

I want to save the output from (void)system into a variable but how exactly would I do that?

Was it helpful?

Solution

You will have to create a pipe and redirect the output from the system call to that pipe, and then read the data from the pipe -- this is what the popen call essentially does, which means that you should just convert the system call to a popen call and then read from the file descriptor into the variables you want to populate.

You can find examples on how to use popen on stack overflow, like this one here; linux command executing by popen on C code

OTHER TIPS

Standard C offers no real solution for this; there are only workarounds. One would be to use shell redirection inside system(), writing the output to a file, then reading that file using fopen(), fread() and friends.

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