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?

有帮助吗?

解决方案

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top