• C

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

LVL 1
Thorn3Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
You can use system() function:

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

Don't miss to specify correctly directoriy paths.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jaime OlivaresSoftware ArchitectCommented:
You will need to include "system.h" header
0
ravs120499Commented:
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
0
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

jkrCommented:
>>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");
0
van_dyCommented:
>>  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");
0
Thorn3Author Commented:
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?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.