1

I am using int res = system("uname -p"); in my c++ code.

It will give the the result in standard output by using

fprintf(stdout,"execution returned %d.\n",res);

I want to store this result string in a variable, I am unable to store it.

I google it but unable to find proper solution, Can any one tell me the correct way.

smitz
  • 79
  • 1
  • 14
  • What you want to store in string varbiale - res or what? – Blood Sep 08 '12 at 13:55
  • 3
    I think he wants to store the output of running `uname -p` in a string variable. – millimoose Sep 08 '12 at 13:56
  • Just to be perfectly clear. I run `uname -p` in a terminal on my computer, and it outputs `i386`. You want to end up with a string variable that contains `i386`, right? If so, you'll need [`popen()`](http://linux.die.net/man/3/popen) and read from the stream it returns. – millimoose Sep 08 '12 at 13:59
  • Possible duplicate of http://stackoverflow.com/questions/125828/capturing-stdout-from-a-system-command-optimally –  Sep 08 '12 at 14:00
  • @millimoose yes i want to store the i386 in a string so that i can use it again – smitz Sep 08 '12 at 14:03

1 Answers1

4

First, you don't need to run the uname command programmatically to get your processor. You can simply run the uname(2) syscall (which the uname command invokes). And you could also read and parse /proc/cpuinfo from your program.

If you wanted to read the output of some command, use popen(3) library function.

See also my answer to a related question.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547