Solved

getting return result from executing

Posted on 2004-09-13
2
231 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C simple state machine for client server handshake 4 299
Need example 5 113
Problem to save 10 163
Using popen() and gunzip() to open file in HTTPServer 6 58
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…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

813 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

13 Experts available now in Live!

Get 1:1 Help Now