Solved

getting return result from executing

Posted on 2004-09-13
2
228 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
2 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 100 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 and use conditional statements in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now