Sending binary data to stdout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gof@mailinator.com

    Sending binary data to stdout

    I'm pretty new to C++, and this seemingly simple thing is driving me
    crazy. I'm trying to write a CGI script to serve images on the fly.
    CGI requires the content to be sent through standard output, but since
    cout and printf convert LF line endings to CRLF (I'm on Windows), the
    resulting image ends up being corrupt.

    Is there ANY way to get around this? It seems writing CGI scripts in
    C++ on Windows is pretty much impossible.

    Here's an example of what I'm trying to do:

    #include <iostream>
    #include <fstream>
    using namespace std;

    int main() {
    ifstream in("in.png", ios::binary);
    cout << "Content-type: image/png\n\n";
    cout << in.rdbuf();
    return 0;
    }

  • Artie Gold

    #2
    Re: Sending binary data to stdout

    gof@mailinator. com wrote:[color=blue]
    > I'm pretty new to C++, and this seemingly simple thing is driving me
    > crazy. I'm trying to write a CGI script to serve images on the fly.
    > CGI requires the content to be sent through standard output, but since
    > cout and printf convert LF line endings to CRLF (I'm on Windows), the
    > resulting image ends up being corrupt.
    >
    > Is there ANY way to get around this? It seems writing CGI scripts in
    > C++ on Windows is pretty much impossible.
    >
    > Here's an example of what I'm trying to do:
    >
    > #include <iostream>
    > #include <fstream>
    > using namespace std;
    >
    > int main() {
    > ifstream in("in.png", ios::binary);
    > cout << "Content-type: image/png\n\n";
    > cout << in.rdbuf();
    > return 0;
    > }
    >[/color]
    This is discussed in the FAQs (which you should have read before
    posting) at: http://www.parashift.com/c++-faq-lite.

    Unfortunately (well, for you ;-)), there is no standard way. You'll have
    to refer to your compiler's documentation or ask in a Windows newsgroup.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)

    Comment

    • Elliot Constantino

      #3
      Re: Sending binary data to stdout

      Thanks for the reply. I did read the relevant bit in the FAQ,
      actually, but I thought it seemed to be saying that only a particular
      way of doing it (reopening cout in binary mode) was impossible, not
      that sending binary data to standard output itself was impossible. Ah
      well. :(

      Comment

      • Larry I Smith

        #4
        Re: Sending binary data to stdout

        gof@mailinator. com wrote:[color=blue]
        > I'm pretty new to C++, and this seemingly simple thing is driving me
        > crazy. I'm trying to write a CGI script to serve images on the fly.
        > CGI requires the content to be sent through standard output, but since
        > cout and printf convert LF line endings to CRLF (I'm on Windows), the
        > resulting image ends up being corrupt.
        >
        > Is there ANY way to get around this? It seems writing CGI scripts in
        > C++ on Windows is pretty much impossible.
        >
        > Here's an example of what I'm trying to do:
        >
        > #include <iostream>
        > #include <fstream>
        > using namespace std;
        >
        > int main() {
        > ifstream in("in.png", ios::binary);
        > cout << "Content-type: image/png\n\n";
        > cout << in.rdbuf();
        > return 0;
        > }
        >[/color]

        If you have to write binary data to STDOUT in 'C', or
        to 'cout' in C++, Windows makes it difficult because of
        the '\r\n' translation done by Windows.

        Ask a Windows newsgroup how to write binary data to
        STDOUT using 'C' from inside a CGI script. However
        that is done will be the same approach used in C++.

        Do you really need to write binary data from your CGI?
        Normally you just write the URL of the image file in your
        web page and the Web Server will fetch/return the image
        without going thru your CGI.

        Regards,
        Larry

        --
        Anti-spam address, change each 'X' to '.' to reply directly.

        Comment

        • Lionel B

          #5
          Re: Sending binary data to stdout

          Elliot Constantino wrote:[color=blue]
          > Thanks for the reply. I did read the relevant bit in the FAQ,
          > actually, but I thought it seemed to be saying that only a particular
          > way of doing it (reopening cout in binary mode) was impossible, not
          > that sending binary data to standard output itself was impossible. Ah
          > well. :(
          >[/color]

          It's not impossible to set cout to binary on Windows. Here's how to do
          it using the MinGW (gcc) compiler; put this code before main()

          #include <fcntl.h> // _O_BINARY
          unsigned int _CRT_fmode = _O_BINARY;

          Not sure about other compilers... you'll have to read the docs.

          HTH,

          --
          Lionel B

          Comment

          • Julián Albo

            #6
            Re: Sending binary data to stdout

            gof@mailinator. com wrote:
            [color=blue]
            > I'm pretty new to C++, and this seemingly simple thing is driving me
            > crazy. I'm trying to write a CGI script to serve images on the fly.
            > CGI requires the content to be sent through standard output, but since
            > cout and printf convert LF line endings to CRLF (I'm on Windows), the
            > resulting image ends up being corrupt.[/color]

            The simpler and portable solution is to use a image format that encodes the
            data image in text lines, there are several of them accepted in all
            browsers. You probably can instruct your web server to send it compressed
            to reduce bandwith usage.

            --
            Salu2

            Comment

            Working...