CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2005
    Posts
    85

    redirecting stdin and stdout programatically

    In order to redirect stdin and stdout in a shell environment I just do this:
    ./exefile < file.in >file.out

    But how can I do this programatically, inside the program, that is, that a program redirect itself its own stdin and stdout??

    thanx.

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: redirecting stdin and stdout programatically

    There is no standard way, but OS specific ways.
    Under Win32, you can use
    SetStdHandle

    Or, when you launch another process, CreateProcess has a STARTUPINFO structure containing a flag, and fields for redirecting stdout, stdin, and stderr.

  3. #3
    Join Date
    Jun 2005
    Posts
    12

    Re: redirecting stdin and stdout programatically

    If we're talking UNIX, look into the dup2() function. You can duplicate open file descriptors (file.in and file.out) across the stdin and stdout descriptors (0 and 1, or STDIN_FILENO and STDOUT_FILENO).

  4. #4
    Join Date
    Jun 2003
    Location
    not-so-Great Britain
    Posts
    178

    Re: redirecting stdin and stdout programatically

    Hungarian notation is the bane of self documenting code.
    Forget all fancy tricks with operator precedence. Code should be easily readable.
    May the BOOST be with you.
    Good free E-Books thanks to Bruce Eckel.

  5. #5
    Join Date
    Jul 2005
    Posts
    85

    Re: redirecting stdin and stdout programatically

    Quote Originally Posted by Improving
    hehe, just that simple, assign a new stream to cin, cout and cerr

  6. #6
    Join Date
    Jun 2003
    Location
    not-so-Great Britain
    Posts
    178

    Re: redirecting stdin and stdout programatically

    read it all.
    Hungarian notation is the bane of self documenting code.
    Forget all fancy tricks with operator precedence. Code should be easily readable.
    May the BOOST be with you.
    Good free E-Books thanks to Bruce Eckel.

  7. #7
    Join Date
    Jul 2005
    Posts
    85

    Re: redirecting stdin and stdout programatically

    Quote Originally Posted by Improving
    read it all.
    Oh, my mistake.
    anyway... it can be done, that's the good thing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured