Link to home
Start Free TrialLog in
Avatar of Thorn3
Thorn3

asked on

Stream Redirection

Hi..

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.

Example
-----------
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.
Cliff

ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You will need to include "system.h" header
Avatar of ravs120499
ravs120499

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 */
exec("replace");
/* 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.

HTH
Ravs
Avatar of jkr
>>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");
>>  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");
Avatar of Thorn3

ASKER

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?