help with redirecting std output & errors

This is a discussion on help with redirecting std output & errors within the Linux Programming forums, part of the Platform Specific Boards category; hi all, i am currently stuck on a project and would appriciate any help. i have to write a spawn ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    5

    help with redirecting std output & errors

    hi all,
    i am currently stuck on a project and would appriciate any help.

    i have to write a spawn program which accepts info from the command line and then sorts out the arguments.

    if -o is an argument then the following arg is the filename where the o/p is sent.

    if -e is present the the next arg is the file name where the errors are sent.

    e.g.

    $ spawn [-o filename] [-e filename] command_sequence

    or
    $ ./spawn -o list.dat ls -l /home/jbloggs

    [New list.dat file contains long listing of the jbloggs directory.]

    any ideas?
    thanks!

  2. #2
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    This sounds like an excercise in the use of freopen(). Here is the prototype:
    Code:
    FILE *freopen (const char *path, const char *mode, FILE *stream);
    freopen() is often used to redirect stdout and stderr. Here is an example:
    Code:
    #include <errno.h>
    #include <stdio.h>
    
    int main( void )
    {
      FILE *NewStdout;
      FILE *NewStderr;
    
      NewStdout = freopen( "outfile", "w", stdout );
      if ( !NewStdout )
      {
        perror( "freopen()" );
        return (errno);
      }
    
      NewStderr = freopen( "errfile", "w", stderr );
      if ( !NewStderr )
      {
        perror( "freopen()" );
        return (errno);
      }
    
      printf( "Testing stdout redirection\n" );
      perror( "Testing stderr redirection" );
    
      fclose( NewStdout );
      fclose( NewStderr );
    
      return 0;
    }
    Jason Deckard

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    in the above program how would you change it to take the argument from the command line.

  4. #4
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Hi Fergie,

    I don't want to give it all away, as this seems to be a homework assignment, but here are a few hints:

    [list=1][*]The first argument of freopen() is the destination for the redirected output.[*]main() can accept the arguments int argc and char **argv (sometimes this is written as: char *argv[]).[/list=1]As your instructor expects you to be able to write a program that uses argc and argv, I'm sure it has been covered in your class. Review your notes on how to pass arguments from the command line and, if you get stuck, reply with some specific questions.

    I'd like to see you complete your assignment, but I also don't want to deprive you of the learning process. :)
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirecting console output
    By _hannes in forum Windows Programming
    Replies: 3
    Last Post: 11-04-2004, 04:51 AM
  2. Redirecting program output straight to an edit control
    By bennyandthejets in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2004, 09:25 AM
  3. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 03:57 PM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. sorting output on student info program
    By indigo0086 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 11:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21