0

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!

infused
  • 24,000
  • 13
  • 68
  • 78
Lorenz
  • 39
  • 6
  • What do you want to save into a variable? Also, include the header `stdio.h` which contains the prototype of `popen`. You should not declare a prototype of standard library functions yourself. – ajay May 01 '14 at 17:08
  • http://stackoverflow.com/questions/4810516/c-redirecting-stdout, though that has some C++ solutions as well – IdeaHat May 01 '14 at 17:13

1 Answers1

0

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

Lallu Anthoor
  • 231
  • 1
  • 5
  • Sorry, i don't know what you mean. Where do i have do put this in my Code/Terminal? When I type this into a terminal it just says: program: command not found – Lorenz May 01 '14 at 17:47