Can I redirect stdout to a file AND the console.

Discussion in 'C Programming' started by Jef Driesen, Nov 23, 2007.

  1. Jef Driesen

    Jef Driesen Guest

    I wrote a program that writes a large amount of information to stdout
    (and stderr). When run from the commandline, this output either appears
    on the console window (the default) or can be redirected to a file by
    means of the freopen() function:

    freopen ("logfile.txt", "w", stdout);

    But I would like to have the output on the console AND a logfile. Is
    this possible (without using an extra program like 'tee')?
    Jef Driesen, Nov 23, 2007
    #1
    1. Advertising

  2. Jef Driesen

    Richard Bos Guest

    Jef Driesen <> wrote:

    > I wrote a program that writes a large amount of information to stdout
    > (and stderr). When run from the commandline, this output either appears
    > on the console window (the default) or can be redirected to a file by
    > means of the freopen() function:
    >
    > freopen ("logfile.txt", "w", stdout);
    >
    > But I would like to have the output on the console AND a logfile. Is
    > this possible (without using an extra program like 'tee')?


    Not without writing it twice.

    Richard
    Richard Bos, Nov 23, 2007
    #2
    1. Advertising

  3. Jef Driesen

    santosh Guest

    In article <fi60in$vh9$>, Jef Driesen
    <> wrote on Friday 23 Nov 2007 1:17 pm:

    > I wrote a program that writes a large amount of information to stdout
    > (and stderr). When run from the commandline, this output either
    > appears on the console window (the default) or can be redirected to a
    > file by means of the freopen() function:
    >
    > freopen ("logfile.txt", "w", stdout);
    >
    > But I would like to have the output on the console AND a logfile. Is
    > this possible (without using an extra program like 'tee')?


    Yes. Duplicate your output to both stderr and to the file. You can use a
    small "wrapper" function to encapsulate this functionality.

    In any case you need to use two different file streams. You can't route
    output to multiple devices by writing to the same stream within
    Standard C. It might be possible with platform specific functions, but
    you need to ask in a group appropriate for your system.
    santosh, Nov 23, 2007
    #3
  4. santosh said:

    > In article <fi60in$vh9$>, Jef Driesen
    > <> wrote on Friday 23 Nov 2007 1:17 pm:
    >

    <snip>
    >>
    >> But I would like to have the output on the console AND a logfile. Is
    >> this possible (without using an extra program like 'tee')?

    >
    > Yes. Duplicate your output to both stderr and to the file. You can use a
    > small "wrapper" function to encapsulate this functionality.


    Something like this, perhaps (but see the note that follows):

    #include <stdio.h>
    #include <stdarg.h>

    int tfprintf(FILE *fpa, FILE *fpb, const char *fmt, ...)
    {
    int rc = 0;
    va_list ap = {0};
    va_start(ap, fmt);
    rc = vfprintf(fpa, fmt, ap);
    va_end(ap);
    if(rc >= 0)
    {
    va_start(ap, fmt);
    rc = vfprintf(fpb, fmt, ap);
    va_end(ap);
    }
    return rc;
    }

    This function's handling of the return values from the two vfprintf calls
    is not particularly satisfactory, but it's hard to see how one could come
    up with a solution that would be pleasing to everyone. Here's one
    possibility:

    int tfprintf(int *rcb, FILE *fpa, FILE *fpb, const char *fmt, ...)

    (with the obvious changes within the function itself), so that the function
    returns the value returned by the first vfprintf, and *rcb is populated
    with the value returned by the second vfprintf.

    Here's another solution:

    struct tfprintf_rt_
    {
    int rca;
    int rcb;
    };

    struct tfprintf_rt_ tfprintf(FILE *fpa, FILE *fpb, const char *fmt, ...)

    And here's another:

    #define TFPRINTF_BOTH_OK 0
    #define TFPRINTF_FAIL1 1
    #define TFPRINTF_FAIL2 2
    #define TFPRINTF_BOTH_BAD (TFPRINTF_FAIL1 | TFPRINTF_FAIL2)

    int tfprintf(struct tfprintf_rt_ *rc, FILE *fpa, FILE *fpb, const char
    *fmt, ...)

    with the return value giving a quick and dirty summary in bitflag form,
    with the details stored in the struct for perusal if required.

    But they all suck, really, don't they? It's just a matter of finding the
    method that sucks *least*, whether it is one of these or some other
    construction - and that's very much a personal style choice.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Nov 23, 2007
    #4
  5. in comp.lang.c i read:

    >I wrote a program that writes a large amount of information to stdout
    >(and stderr).


    >But I would like to have the output on the console AND a logfile. Is
    >this possible (without using an extra program like 'tee')?


    in addition to leaving it to the person using your program to arrange for
    it and the other response(s) (output twice), it may be worth noting that
    the semantics of the filename parameter is implementation defined so it may
    be possible without any more effort than the formulation of the argument
    value. but i wouldn't count on it, and a strictly conforming program
    cannot, so perhaps it is best to find another way.

    --
    a signature
    those who know me have no need of my name, Nov 24, 2007
    #5
  6. Jef Driesen

    CBFalconer Guest

    those who know me have no need of my name wrote:
    > Jef Driesen wrote:
    >
    >> I wrote a program that writes a large amount of information to
    >> stdout (and stderr).

    >
    >> But I would like to have the output on the console AND a logfile.
    >> Is this possible (without using an extra program like 'tee')?

    >
    > in addition to leaving it to the person using your program to
    > arrange for it and the other response(s) (output twice), it may
    > be worth noting that the semantics of the filename parameter is
    > implementation defined so it may be possible without any more
    > effort than the formulation of the argument value. but i
    > wouldn't count on it, and a strictly conforming program cannot,
    > so perhaps it is best to find another way.


    Simple. Write the program with a 'putdouble' routine, that writes
    a char to both of two separate files. Let the command structure
    select those files. You can also arrange to have default names,
    which may include stdout and null.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Nov 24, 2007
    #6
  7. Jef Driesen

    Jef Driesen Guest

    those who know me have no need of my name wrote:
    > in comp.lang.c i read:
    >
    >> I wrote a program that writes a large amount of information to stdout
    >> (and stderr).

    >
    >> But I would like to have the output on the console AND a logfile. Is
    >> this possible (without using an extra program like 'tee')?

    >
    > in addition to leaving it to the person using your program to arrange for
    > it and the other response(s) (output twice), it may be worth noting that
    > the semantics of the filename parameter is implementation defined so it may
    > be possible without any more effort than the formulation of the argument
    > value. but i wouldn't count on it, and a strictly conforming program
    > cannot, so perhaps it is best to find another way.


    The problem is that I have an application that needs to be tested by a
    number of people and they have to send the results back to me. Therefore
    I redirect stdout and stderr to a file that is easy to send. But that
    way, nothing appears on the console which gives the impression there is
    no progress.

    The test persons are not always very skilled computer users, so I want
    to make it as easy as possible to run the test. Therefore I want to
    avoid to explain how to redirect output them self. Also my testers are
    mostly running Windows and they do not have a 'tee' utility.
    Jef Driesen, Nov 24, 2007
    #7
  8. Jef Driesen

    Jef Driesen Guest

    santosh wrote:
    > In article <fi60in$vh9$>, Jef Driesen
    > <> wrote on Friday 23 Nov 2007 1:17 pm:
    >
    >> I wrote a program that writes a large amount of information to stdout
    >> (and stderr). When run from the commandline, this output either
    >> appears on the console window (the default) or can be redirected to a
    >> file by means of the freopen() function:
    >>
    >> freopen ("logfile.txt", "w", stdout);
    >>
    >> But I would like to have the output on the console AND a logfile. Is
    >> this possible (without using an extra program like 'tee')?

    >
    > Yes. Duplicate your output to both stderr and to the file. You can use a
    > small "wrapper" function to encapsulate this functionality.


    That requires rewriting my application (and all libraries it depends
    on). That is something I wanted to avoid because I need it for testing.

    > In any case you need to use two different file streams. You can't route
    > output to multiple devices by writing to the same stream within
    > Standard C. It might be possible with platform specific functions, but
    > you need to ask in a group appropriate for your system.


    Do you happen to know if it can be done on Windows? I can't find
    anything, but I don't know what function to look after.
    Jef Driesen, Nov 24, 2007
    #8
  9. In article <FY_1j.223213$-ops.be>,
    Jef Driesen <> wrote:
    >those who know me have no need of my name wrote:
    >> in comp.lang.c i read:
    >>
    >>> I wrote a program that writes a large amount of information to stdout
    >>> (and stderr).

    >>
    >>> But I would like to have the output on the console AND a logfile. Is
    >>> this possible (without using an extra program like 'tee')?

    >>
    >> in addition to leaving it to the person using your program to arrange for
    >> it and the other response(s) (output twice), it may be worth noting that
    >> the semantics of the filename parameter is implementation defined so it may
    >> be possible without any more effort than the formulation of the argument
    >> value. but i wouldn't count on it, and a strictly conforming program
    >> cannot, so perhaps it is best to find another way.

    >
    >The problem is that I have an application that needs to be tested by a
    >number of people and they have to send the results back to me. Therefore
    >I redirect stdout and stderr to a file that is easy to send. But that
    >way, nothing appears on the console which gives the impression there is
    >no progress.
    >
    >The test persons are not always very skilled computer users, so I want
    >to make it as easy as possible to run the test. Therefore I want to
    >avoid to explain how to redirect output them self. Also my testers are
    >mostly running Windows and they do not have a 'tee' utility.


    I Expect there is a simple solution to this...

    http://bmrc.berkeley.edu/people/chaffee/expectnt.html

    It's a little old (and, of course, completely OT [heh heh]), but it
    works very well. I've used it for years.
    Kenny McCormack, Nov 24, 2007
    #9
  10. Jef Driesen

    CBFalconer Guest

    Jef Driesen wrote:
    >

    .... snip ...
    >
    > The test persons are not always very skilled computer users, so I
    > want to make it as easy as possible to run the test. Therefore I
    > want to avoid to explain how to redirect output them self. Also my
    > testers are mostly running Windows and they do not have a 'tee'
    > utility.


    So write one. It isn't hard.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Nov 24, 2007
    #10
  11. Jef Driesen

    Jack Klein Guest

    On Sat, 24 Nov 2007 19:29:41 GMT, Jef Driesen
    <> wrote in comp.lang.c:

    > those who know me have no need of my name wrote:
    > > in comp.lang.c i read:
    > >
    > >> I wrote a program that writes a large amount of information to stdout
    > >> (and stderr).

    > >
    > >> But I would like to have the output on the console AND a logfile. Is
    > >> this possible (without using an extra program like 'tee')?

    > >
    > > in addition to leaving it to the person using your program to arrange for
    > > it and the other response(s) (output twice), it may be worth noting that
    > > the semantics of the filename parameter is implementation defined so it may
    > > be possible without any more effort than the formulation of the argument
    > > value. but i wouldn't count on it, and a strictly conforming program
    > > cannot, so perhaps it is best to find another way.

    >
    > The problem is that I have an application that needs to be tested by a
    > number of people and they have to send the results back to me. Therefore
    > I redirect stdout and stderr to a file that is easy to send. But that
    > way, nothing appears on the console which gives the impression there is
    > no progress.
    >
    > The test persons are not always very skilled computer users, so I want
    > to make it as easy as possible to run the test. Therefore I want to
    > avoid to explain how to redirect output them self. Also my testers are
    > mostly running Windows and they do not have a 'tee' utility.


    So why not give them a "tee" utility, and a batch file to use it to
    invoke the program you want them to test? As Chuck pointed out, it's
    not at all hard to write one. And there are almost certainly quite a
    few freeware ones floating around for Windows.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 25, 2007
    #11
    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. 28tommy
    Replies:
    2
    Views:
    11,232
    28tommy
    Dec 29, 2005
  2. Elad
    Replies:
    0
    Views:
    397
  3. ids
    Replies:
    4
    Views:
    1,476
    Keith Thompson
    Apr 14, 2009
  4. Sal
    Replies:
    1
    Views:
    372
  5. Replies:
    13
    Views:
    1,221
    Anno Siegel
    Nov 30, 2005
Loading...

Share This Page