Stream Redirection. Newbies question

Discussion in 'C Programming' started by Alexander Baranov, Oct 25, 2004.

  1. Hi.
    I work in Debian Linux GCC. I have a function, which opens a socket and
    binds 'mystream' stream with this socket (by fdopen command). Then, while
    being inside this function I want stdout be redirected to mystream. On
    exiting the function I have to return stdout to ordinary stdout.
    I did it like this:

    Entering function
    {
    ...
    FILE* sos_stdout;
    sos_stdout = stdout; //reserving stdout
    stdout = mystream; //redirecting
    ....
    doing something
    ....
    stdout = sos_stdout; //returning stdout back
    fclose(mystream); //closing my stream
    .....
    Exiting..
    }

    It works, but I still have a feeling that I do it in a wrong way. Please,
    advise if it is really so.
    Regards, Alex.
    Alexander Baranov, Oct 25, 2004
    #1
    1. Advertising

  2. Alexander Baranov wrote:
    [...]
    > FILE* sos_stdout;
    > sos_stdout = stdout; //reserving stdout
    > stdout = mystream; //redirecting


    This statement won't work on all platforms, as "stdout" may not be an
    lvalue. For example, several of my systems have stdio.h lines like:

    #define stdin (&__iob[0])
    #define stdout (&__iob[1])
    #define stderr (&__iob[2])

    [...]

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <>
    Kenneth Brody, Oct 25, 2004
    #2
    1. Advertising

  3. Alexander Baranov

    Dan Pop Guest

    In <81066$417cfa3c$44a55b44$> "Alexander Baranov" <> writes:

    >Entering function
    >{
    >..
    >FILE* sos_stdout;
    >sos_stdout = stdout; //reserving stdout
    >stdout = mystream; //redirecting
    >...
    >doing something
    >...
    >stdout = sos_stdout; //returning stdout back
    >fclose(mystream); //closing my stream
    >....
    >Exiting..
    >}
    >
    >It works, but I still have a feeling that I do it in a wrong way. Please,
    >advise if it is really so.


    You're right. stdin, stdout and stderr need not be modifiable lvalues,
    so you may not be allowed to assign anything to them (even if it works
    on your particular implementation).

    Use fprintf on mystream instead of printf (ditto for fputs instead puts)
    and you don't have to mess with stdout.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
    Dan Pop, Oct 25, 2004
    #3
    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. Joona I Palaste

    Question about newbies and interfaces

    Joona I Palaste, Nov 30, 2003, in forum: Java
    Replies:
    2
    Views:
    335
    Chris Uppal
    Nov 30, 2003
  2. Chinmoy Mukherjee

    newbies question about return value

    Chinmoy Mukherjee, Jun 14, 2005, in forum: C++
    Replies:
    3
    Views:
    309
    Jim Langston
    Jun 15, 2005
  3. Vivian

    Newbies Question on data variables

    Vivian, Jul 12, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    310
    Vivian
    Jul 12, 2006
  4. Alf P. Steinbach

    How we recognize newbies as newbies

    Alf P. Steinbach, Aug 13, 2007, in forum: C++
    Replies:
    31
    Views:
    1,102
    Diego Martins
    Aug 21, 2007
  5. Marcin Tyman

    telnet and redirection of stream

    Marcin Tyman, Jul 20, 2007, in forum: Ruby
    Replies:
    0
    Views:
    77
    Marcin Tyman
    Jul 20, 2007
Loading...

Share This Page