how to write binary data to stdout?

Discussion in 'C Programming' started by Rouben Rostamian, Oct 19, 2003.

  1. Consider the following demo program:

    #include <stdio.h>

    int main(void)
    {
    unsigned char c;

    for (c=0; c<15; c++)
    putchar(c);
    return 0;
    }

    As is, the output of the program is platform-dependent.
    For instance, in the UNIX environment, it puts 15 characters
    to stdout. Under windows it puts out at least 16, because c=10
    gets translated to newline+carriage return.

    This brings me to:

    Question: Is it possible to close stdout then reopen it in binary
    mode in a platform-independent way?

    If that were possible, then the binary-mode output of the program
    would be the same on all platforms.

    The purpose of my real program (not this demo,) is to act as a
    filter from stdin to stdout, therefore fopen(filename, "wb") is
    of not much help.

    --
    Rouben Rostamian <>
     
    Rouben Rostamian, Oct 19, 2003
    #1
    1. Advertising

  2. Rouben Rostamian

    James Hu Guest

    On 2003-10-18, Rouben Rostamian <> wrote:
    > Question: Is it possible to close stdout then reopen it in binary
    > mode in a platform-independent way?


    No.

    If you are seeking a completely standard solution, you are better off
    outputting a text encoding of the binary data. If you are writing
    programs intended to read each others input/output, the receiving
    program would need to decode the stream.

    -- James
     
    James Hu, Oct 19, 2003
    #2
    1. Advertising

  3. (Rouben Rostamian) wrote:

    <snip>
    >Question: Is it possible to close stdout then reopen it in binary
    >mode in a platform-independent way?


    Well, there are two problems:

    1) stdout need not be a modifiable lvalue, so you can't just
    use fopen - you cannot come up with a suitable filename anyway.
    To solve this, one could come up with
    reopen( NULL, "wb", stdout);
    running into problem number

    2) It is implementation defined which (if any) and under what
    circumstances mode changes are permitted.

    >If that were possible, then the binary-mode output of the program
    >would be the same on all platforms.


    That would be great, but unfortunately it's impossible. stdin,
    stdout and stderr are text streams per definition, and there is
    no portable way to switch them to binary mode.

    >The purpose of my real program (not this demo,) is to act as a
    >filter from stdin to stdout, therefore fopen(filename, "wb") is
    >of not much help.


    I cannot come up with a better solution, sorry.

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 19, 2003
    #3
  4. Rouben Rostamian

    CBFalconer Guest

    Rouben Rostamian wrote:
    >
    > Consider the following demo program:
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > unsigned char c;
    > for (c=0; c<15; c++) putchar(c);
    > return 0;
    > }
    >
    > As is, the output of the program is platform-dependent.
    > For instance, in the UNIX environment, it puts 15 characters
    > to stdout. Under windows it puts out at least 16, because c=10
    > gets translated to newline+carriage return.
    >
    > This brings me to:
    >
    > Question: Is it possible to close stdout then reopen it in binary
    > mode in a platform-independent way?
    >
    > If that were possible, then the binary-mode output of the program
    > would be the same on all platforms.
    >
    > The purpose of my real program (not this demo,) is to act as a
    > filter from stdin to stdout, therefore fopen(filename, "wb") is
    > of not much help.


    This is handled by freopen.

    if (freopen(NULL, "wb", stdout)) {...}

    Note that it may fail, and has some implementation dependancies.
    >From N869:


    7.19.5.4 The freopen function

    Synopsis
    [#1]
    #include <stdio.h>
    FILE *freopen(const char * filename,
    const char * mode,
    FILE * restrict stream);

    Description

    [#2] The freopen function opens the file whose name is the
    string pointed to by filename and associates the stream
    pointed to by stream with it. The mode argument is used
    just as in the fopen function.215)

    ____________________

    215The primary use of the freopen function is to change the
    file associated with a standard text stream (stderr,
    stdin, or stdout), as those identifiers need not be
    modifiable lvalues to which the value returned by the
    fopen function may be assigned.


    [#3] If filename is a null pointer, the freopen function
    attempts to change the mode of the stream to that specified
    by mode, as if the name of the file currently associated
    with the stream had been used. It is implementation-defined
    which changes of mode are permitted (if any), and under what
    circumstances.

    [#4] The freopen function first attempts to close any file
    that is associated with the specified stream. Failure to
    close the file is ignored. The error and end-of-file
    indicators for the stream are cleared.

    Returns

    [#5] The freopen function returns a null pointer if the open
    operation fails. Otherwise, freopen returns the value of
    stream.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 19, 2003
    #4
  5. Rouben Rostamian

    Richard Bos Guest

    CBFalconer <> wrote:

    > Rouben Rostamian wrote:
    >
    > > Question: Is it possible to close stdout then reopen it in binary
    > > mode in a platform-independent way?

    >
    > This is handled by freopen.
    >
    > if (freopen(NULL, "wb", stdout)) {...}
    >
    > Note that it may fail, and has some implementation dependancies.
    > >From N869:


    One of which is that a null first argument to freopen() is a C99
    feature.

    Richard
     
    Richard Bos, Oct 21, 2003
    #5
    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. Replies:
    5
    Views:
    1,528
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    May 14, 2005
  2. John Forkosh

    binary output to stdout in Windows

    John Forkosh, Feb 15, 2004, in forum: C Programming
    Replies:
    8
    Views:
    955
    John Forkosh
    Feb 16, 2004
  3. Elad
    Replies:
    0
    Views:
    425
  4. Andreas S
    Replies:
    3
    Views:
    280
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    365
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page