Sending binary data to stdout

Discussion in 'C++' started by gof, May 14, 2005.

  1. gof

    gof Guest

    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;
    gof, May 14, 2005
    1. Advertisements

  2. gof

    Artie Gold Guest

    This is discussed in the FAQs (which you should have read before
    posting) at:

    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.

    Artie Gold, May 14, 2005
    1. Advertisements

  3. 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. :(
    Elliot Constantino, May 14, 2005
  4. 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.

    Larry I Smith, May 14, 2005
  5. gof

    Lionel B Guest

    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.

    Lionel B, May 14, 2005
  6. 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.
    =?ISO-8859-15?Q?Juli=E1n?= Albo, May 14, 2005
    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.