Stream Redirection

Posted on 2004-11-08
Medium Priority
Last Modified: 2010-04-15

I am writing some software which takes a list of files and converts a bunch of text in them.

I have wrote a couple modules already which takes data from stdinput, operates on it and outputs to the stdoutput. I did it this way as it is flexable and the invididual modules are helpful on my system.

I am now working on a program which takes all of my executable modules and will run them according to a template supplied but I do not know how to call a program within C and be able to post data to stdin and read the result from stdout. I am using C code and do not wish to do this using batch scripts or such.

I Have 1 file called test.mpr containing "Good Morning World "
1 Module called replace which takes 2 arguments <searchstring> <replacestring>

I can accomplish what I want from a terminal like so.
cat test.mpr | replace "Morning" "Night" > output.mpr
output.mpr now will contain "Good Night World"

How can I accomplish the same thing using strictly C?
I would like to keep my code as cross compatible as possible aswell.

Im sure its an simple task I just can't seem to find exactly what im looking for.

Thanks in advance.

Question by:Thorn3
LVL 55

Accepted Solution

Jaime Olivares earned 500 total points
ID: 12532568
You can use system() function:

system("someprogram <someinput.txt >someoutput.txt");

Don't miss to specify correctly directoriy paths.
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12532581
You will need to include "system.h" header

Expert Comment

ID: 12533649
If you want to dig deeper into C library, then I think this how a shell (written in C, obviously) would implement IO redirection. You can do the same...

/* Open the input and output files. */
int fd_in, fd_out;

fd_in = open("test.mpr", O_RDONLY);
/* check error */

fd_out = open("output.mpr", O_WRONLY);
/* check error */

/* Replace stdin and stdout */
dup2(fd_in, 0) /* 0 is the file descriptor for stdin */
/* check error */

dup2(fd_out, 1) /* stdout */
/* check error */

/* Run the "replace" program */
/* check error */

One variant of the above code is - if the input/output stream has to be redirected to another program rather than a file, then use the pipe() call to create a pipe and use the file descriptors of the pipe in the dup2() call.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 86

Expert Comment

ID: 12534170
>>How can I accomplish the same thing using strictly C?

You can use 'system()', but not like jaime_olivares told you to. As the redirection is done by the shell (and not by any other program), you need to invoke a shell and ask it for that, e.g.

system("/bin/sh -c cat test.mpr | replace \"Morning\" \"Night\" > output.mpr");

Expert Comment

ID: 12535244
>>  As the redirection is done by the shell (and not by any other program), you need to invoke a shell and ask it for that, e.g.

thats not right, system() function is _not_ an interface into the operating
system, but rather its an interface to the shell(under unix environment)
itself. POSIX 2. specifies that system() shall return success only if a command
interpreter is availiable in the environment(command interpreter = shell in our case).
Hence its perfectly OK to invoke system as:

system("someprogram <someinput.txt >someoutput.txt");

Author Comment

ID: 12541114
Thank you for the help..

The answer is not quite what I was hopeing for but should work.

On a side note how can bash do redirection internally after the user types in a command like this for instance "echo hello | cat". Does it make OS Specific API calls?

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

850 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