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.

5
  • What you want to store in string varbiale - res or what? Commented Sep 8, 2012 at 13:55
  • 3
    I think he wants to store the output of running uname -p in a string variable. Commented Sep 8, 2012 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() and read from the stream it returns. Commented Sep 8, 2012 at 13:59
  • Possible duplicate of stackoverflow.com/questions/125828/… Commented Sep 8, 2012 at 14:00
  • @millimoose yes i want to store the i386 in a string so that i can use it again Commented Sep 8, 2012 at 14:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

hey thanks millimoose & BasileStarynkevitch the popen() works for me Great !!! :)
But in your case you really should use uname(2); doing a popen is ridiculous for that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.