Stream Redirection

Posted on 2004-11-08
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

    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
    You will need to include "system.h" header
    LVL 7

    Expert Comment

    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.

    LVL 86

    Expert Comment

    >>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");
    LVL 5

    Expert Comment

    >>  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");
    LVL 1

    Author Comment

    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

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    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…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now