문제

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!

도움이 되었습니까?

해결책

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.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top