[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

getting return result from executing

Posted on 2004-09-13
2
Medium Priority
?
249 Views
Last Modified: 2010-05-18
Hi, I need to execute another program from within my program and capture the result.  For example:

executing "bin/date +%H" will return "14" if it is 2 o clock
i need to capture the "14" into a variable int hr

i tried this:

int hr=execve("/bin/date","+%H",envp);

but all that does is run the date program, output the "14" and then it does not return a value.

I know that execve does not return a value unless it fails.

What command can I use to execute another command and get the output of it?

Sorry so long.
Thanks,
Josh
0
Comment
Question by:jsm11482
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 400 total points
ID: 12047785
You need to run popen("/bin/date +%H", "r") instead.
This runs the program, redirecting stdout to a pipe that
you can then read.

int hour = 0;
FILE * fp = popen("/bin/date +%H", "r");
if (fp) {
  fscanf(fp, "%d", &hour);
  pclose(fp);
}


0
 

Author Comment

by:jsm11482
ID: 12047873
perfect, thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question