redirecting stdout

Discussion in 'C Programming' started by jseb, Jul 16, 2013.

  1. No.

    Please re-read.

    Then get back to us, mmK?
     
    Kenny McCormack, Jul 17, 2013
    #21
    1. Advertisements

  2. OK - I'll see if I can dig it up. I need to find an old disk image and see
    if it is still there.
    Yaknow... And FWIW, and all that...

    It just occurred to me, why not just use "freopen()"?
    Something like:

    freopen("/tmp/mytmpfile","w",stdout);
    // Invoke whatever things generate output on stdout
    // Now read /tmp/mytmpfile into memory, parse it, do whatever you need to.
    // (And then unlink() the file)

    This is not quite as cool as having it write directly to memory, but it is
    pretty simple to implement.
     
    Kenny McCormack, Jul 17, 2013
    #22
    1. Advertisements

  3. jseb

    Tim Rentsch Guest

    Quoting the person I was responding to (and who started the
    thread), whose comment you snipped:

    I don't see any reason for anyone to believe you have a better
    understanding of what he is trying to do than he does. If and
    when he has something to say in response to my remarks, I shall
    consider what further response might be appropriate at that
    time. And he is welcome to email me directly at the above
    address if he might prefer that.
     
    Tim Rentsch, Jul 17, 2013
    #23
  4. (snip)
    Back when I used to use SunOS (pre-Solaris), they had device
    mapped memory. That is, a /dev entry that, when opened, would
    read form or write to memory. (fseek() is very useful here.)

    Also, the more usual memmap for memory mapped files, along with
    the devices allowed one to map memory to memory. In the specific
    case, we could map some part of VME address space (VME has a lot of
    different address space) to user memory, allowing for easy use
    of VME cards with memory mapped I/O or on-board buffers.

    I don't know if current systems do that, though.

    -- glen
     
    glen herrmannsfeldt, Jul 17, 2013
    #24
  5. jseb

    Phil Carmody Guest

    Can you even do that? stdout isn't necessarily an lvalue, is it?

    And do you even want to do that - shouldn't you close stdout first?

    Phil
     
    Phil Carmody, Jul 23, 2013
    #25
  6. OK - I finally got around to unearthing that old code that I wrote.

    The idea is that you have a process (program) that generates output on
    stdout and you want to capture that in buffer (i.e., a string), without
    having to re-write any of the code that generates it. I.e., as far as the
    process is concerned, it *is* writing to stdout. Note that the stdout
    generating process may be any combination of puts, printf, etc.

    Here's the code:

    EXPORT char *mp3_info(int argc,char **argv,int olen,char *obuff,int elen,char *ebuff)
    {
    static char buff[30];
    int ret;

    setvbuf(stdout,obuff,_IOFBF,olen);
    setvbuf(stderr,ebuff,_IOFBF,elen);
    optind = 1;
    ret = main(argc,argv);
    obuff[olen = stdout -> _cnt ? olen - stdout -> _cnt : 0] = 0;
    ebuff[elen = stderr -> _cnt ? elen - stderr -> _cnt : 0] = 0;
    sprintf(buff,"%d,%d,%d",ret,olen,elen);
    return buff;
    }

    Explanation:
    1) This is a Windows DLL, hence the "EXPORT".
    2) It is called from a program written in a scripting language; so the actual
    return value is a string containing "ret", "olen", and "elen". This
    detail need not concern us in the instant context.
    3) The caller passes in the argc and argv values which are then passed to
    "main" to do the actual work. The caller also passes in buffers and
    lengths for stdout and stderr, which are captured independently.

    Tell me what you think...
     
    Kenny McCormack, Jul 25, 2013
    #26
  7. jseb

    James Kuyper Guest

    Which approach?

    I know the faulty reasons that you have given for not providing proper
    attribution of quoted remarks (and, as always, I refuse permission to
    quote my text without proper attribution). However, responding to
    something without even quoting it isn't the solution.
     
    James Kuyper, Jul 25, 2013
    #27
  8. Of course. I made it entirely clear that this was a very platform-specific
    approach - and the OP seemed OK with that.

    It's based on, as I noted in my original post to this thread, "cracking
    open stdio.h" and figuring it out. Obviously, you may have to re-do that
    effort to get it to work on another platform (compiler, toolchain, etc).

    But it works - and that, as they say, is the important thing.

    --
    But the Bush apologists hope that you won't remember all that. And they
    also have a theory, which I've been hearing more and more - namely,
    that President Obama, though not yet in office or even elected, caused the
    2008 slump. You see, people were worried in advance about his future
    policies, and that's what caused the economy to tank. Seriously.

    (Paul Krugman - Addicted to Bush)
     
    Kenny McCormack, Jul 25, 2013
    #28
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.