Question

I want to save the output of a system command in a variable, to use it for a GTKLabel.

I know that I can use popen to record the output like this:

FILE *in;
extern FILE *popen();
char buff[512];
char test[512];

if(!(in = popen("adb devices", "r"))){
   exit(1);
}

while(fgets(buff, sizeof(buff), in)!=NULL){
    printf("%s", buff);
}

pclose(in);

So now, it does only print the Output, but I want to save it into a varible. How do i do that? Thanks in Advance community!

Was it helpful?

Solution

You can use output redirection for this. It works perfectly in command line, and should work good in a program as well.

$ program > outputfile

This will record the output of the program to the file outputfile. You can later read this using normal fopen command. (The dollar is the linux prompt.)

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