문제

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