topics that matter; ideas worth sharing

share a tip, submit a link, add something new

Redirecting I/O Programmatically

July 5, 2002, 12:00 AM —  ITworld — 

Last week, I showed you how to redirect a program's input or output from
the command line. Today, we will see how to accomplish the same task
programmatically.

The popen() Syscall
The system() command launches a new child process allowing it to read
from the standard input and write to the standard output. However, in
many cases, you want the child process to read input from the parent
process or direct its output to the parent process instead of the
standard output. The popen() syscall enables you to achieve this:

FILE * popen(const char * command, const char * mode);

The first parameter is the command the name of the executable to be
launched. The second parameter should be "r" if the parent wants to read
the output from child, or "w" if it wishes to write to the child's
standard input (you can't combine both at once with popen()). popen()
returns FILE*, which can be read from or written to, or NULL if the call
fails. Just as with ordinary stdio files, you must close the pipe by
calling pclose() after the I/O operation is completed. In addition, you
should flush the stream after writing to it to prevent delays due to
stdio buffering.

The following program launches a child process called "converter" that
converts Fahrenheit degrees to Celsius. The parent process passes
converter an expression for evaluation and waits for it to evaluate it.
Finally, the parent process closes the pipe and exits too:

#include
#include
#include

int main()
{
char buff[1024]={0};
FILE * cvt;
int status;
/*launch converter and open a pipe through which the
parent will write to it */
cvt=popen("converter", "w");
if (!cvt)
{
printf("couldn't open a pipe; quitting\n");
exit(1)
}
printf("enter Fahrenheit degrees: " );
fgets(buff, sizeof (buff), stdin); /*read user's input*/
/*send expression to converter for evaluation*/
fprintf(cvt, "%s\n", buff);
fflush(cvt);
/*close pipe to converter and wait for it to exit*/
status=pclose(cvt);
/*check the exit status of pclose()*/
if (!WIFEXITED(status))
printf("error on closing the pipe\n");
return 0;
}

The important steps in this program are:

1) The popen() call which establishes the association between a
child process and a pipe in the parent.
2) The fprintf() call that uses the pipe as an ordinary file to
write to the child process's stdin or read from its stdout.
3) The pclose() call that closes the pipe and causes the child
process to terminate.

» posted by ITworld staff

ITworld

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Resources
White Paper

Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.

Webcast

Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.

White Paper

Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.

Free stuff
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

More Resources