Solved

getting return result from executing

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
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.

691 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