Sending binary data to stdout

G

gof

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;
}
 
A

Artie Gold

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;
}
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
 
E

Elliot Constantino

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. :(
 
L

Larry I Smith

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;
}

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
 
L

Lionel B

Elliot said:
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. :(

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,
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

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.

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top