problems redirecting an open file to stdin using dup2()

Discussion in 'C Programming' started by DyslexicAnaboko, Dec 5, 2006.

  1. Hello,

    I have a module that is part of larger project that is giving me
    trouble, so I setup an example.

    Brief
    =====

    I simply want to open a text file and make the contents avaliable
    through stdin that way a bourne shell script I wrote will execute
    properly. The shell script is expecting input from stdin.

    This program will be executing on a UNIX machine and communicating
    with the shell.

    Code
    =====START======

    #include <stdio.h>
    #include <stdlib.h>

    const char cmd[] = "./proj4.sh" ; /* Shell Script */
    const char FILE_NAME[] = "tf.txt" ; /* File to open */

    int main()
    {
    FILE *fp ;

    /* Open File For Standard Input */
    if( ( fp = fopen( FILE_NAME, "r" ) ) == NULL )
    {
    printf( "Cannot Open File: %s", FILE_NAME ) ;
    exit( 1 ) ;
    }

    if( fp != 0 ) /* If fp isn't stdin */
    {
    dup2( fp, 0 ); /* Make fp stdin */
    close( fp ) ; /* Close original fp */
    }

    printf( "\n\nRunning Line Now...\n\n" ) ;

    execl( "/bin/sh", "sh", "-c", cmd, (char *)0 ) ;
    }

    =====STOP=======

    Essentially the effect I am looking for here is:

    ../proj4.sh < tf.txt

    I cannot get this to execute period. Either I get a compiler error, or
    if I change fopen() for open() and make fp an int, the program
    executes, but my shell script does nothing, meaning it didn't recieve
    anything from stdin.

    I would appreciate the help if anyone is will to work with me.

    Thank You for your time,

    Eli Hayon
    DyslexicAnaboko, Dec 5, 2006
    #1
    1. Advertising

  2. DyslexicAnaboko

    Chris Torek Guest

    In article <>
    DyslexicAnaboko <> wrote:
    >This program will be executing on a UNIX machine and communicating
    >with the shell.


    You want comp.unix.programmer <OT>where we will point out the
    fileno() function/macro and perhaps ask for more information
    about the version using open()</OT>.

    [snippage]
    > /* Open File For Standard Input */
    > if( ( fp = fopen( FILE_NAME, "r" ) ) == NULL )
    > {
    > printf( "Cannot Open File: %s", FILE_NAME ) ;
    > exit( 1 ) ;
    > }
    >
    > if( fp != 0 ) /* If fp isn't stdin */


    This compares fp to NULL. It cannot be equal to NULL, since you
    already took care of that case earlier.

    > {
    > dup2( fp, 0 ); /* Make fp stdin */
    > close( fp ) ; /* Close original fp */
    > }


    These are not Standard C functions <OT>and do not take "FILE *"
    objects anyway</OT>.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Dec 5, 2006
    #2
    1. Advertising

  3. DyslexicAnaboko

    santosh Guest

    DyslexicAnaboko wrote:
    > Hello,
    >
    > I have a module that is part of larger project that is giving me
    > trouble, so I setup an example.
    >
    > Brief
    > =====
    >
    > I simply want to open a text file and make the contents avaliable
    > through stdin that way a bourne shell script I wrote will execute
    > properly. The shell script is expecting input from stdin.
    >
    > This program will be executing on a UNIX machine and communicating
    > with the shell.


    This question is more pertinent to comp.unix.programmer,
    comp.os.linux.* etc.
    santosh, Dec 5, 2006
    #3
  4. DyslexicAnaboko

    SM Ryan Guest

    "DyslexicAnaboko" <> wrote:
    # Hello,
    #
    # I have a module that is part of larger project that is giving me
    # trouble, so I setup an example.
    #
    # Brief
    # =====
    #
    # I simply want to open a text file and make the contents avaliable
    # through stdin that way a bourne shell script I wrote will execute
    # properly. The shell script is expecting input from stdin.

    How about freopen?
    freopen("file path","r",stdin)
    should close the file associated with stdin and then reopen stdin
    associated to the "file path".


    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Who's leading this mob?
    SM Ryan, Dec 5, 2006
    #4
  5. SM Ryan wrote:
    > "DyslexicAnaboko" <> wrote:
    > # Hello,
    > #
    > # I have a module that is part of larger project that is giving me
    > # trouble, so I setup an example.
    > #
    > # Brief
    > # =====
    > #
    > # I simply want to open a text file and make the contents avaliable
    > # through stdin that way a bourne shell script I wrote will execute
    > # properly. The shell script is expecting input from stdin.
    >
    > How about freopen?
    > freopen("file path","r",stdin)
    > should close the file associated with stdin and then reopen stdin
    > associated to the "file path".
    >
    >
    > --
    > SM Ryan http://www.rawbw.com/~wyrmwif/
    > Who's leading this mob?


    I tried frepoen, maybe I used it incorrectly, but I get the same
    result, nothing happens.

    I am going to repost in the correct group though, sorry about that, it
    didn't occur to me that I was posting to the wrong group, thanks so far
    though.

    Eli
    DyslexicAnaboko, Dec 5, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PC

    pipe() dup2() fork() execl() select()

    PC, Aug 9, 2003, in forum: C Programming
    Replies:
    3
    Views:
    1,239
  2. Help with open, dup and dup2

    , Mar 16, 2006, in forum: C Programming
    Replies:
    2
    Views:
    367
    Ian Collins
    Mar 16, 2006
  3. David Thompson

    Re: dup2, fclose and rename ...

    David Thompson, Sep 1, 2008, in forum: C Programming
    Replies:
    0
    Views:
    426
    David Thompson
    Sep 1, 2008
  4. candide

    Redirecting stdin to a file

    candide, Dec 4, 2009, in forum: Python
    Replies:
    3
    Views:
    645
    candide
    Dec 5, 2009
  5. Hongliang Wang

    dup2

    Hongliang Wang, Apr 22, 2014, in forum: C++
    Replies:
    14
    Views:
    127
    Hongliang Wang
    Apr 30, 2014
Loading...

Share This Page