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;
} 5 6712 go*@mailinator. com wrote: 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
--
Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays
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. :( go*@mailinator. com wrote: 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
--
Anti-spam address, change each 'X' to '.' to reply directly.
Elliot Constantino wrote: 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,
--
Lionel B go*@mailinator. com wrote: 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.
--
Salu2 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: coder_1024 |
last post by:
I'm trying to send a packet of binary data to a UDP server. If I send
a text string, it works fine. If I attempt to send binary data, it
sends a UDP packet with 0 bytes of data (just the headers). I can see
this because I'm running Ethereal and watching the packets.
I'm defining the packets as shown below:
$text_msg = "Hello, world\r\n";
$binary_msg = chr(0x01).chr(0x02).chr(0x03).chr(0x00).chr(0xA0);
$binary_msg_size = 5;
|
by: Paul Watson |
last post by:
How can I write lines to stdout on a Windows machine without having '\n'
expanded to '\r\n'.
I need to do this on Python 2.1 and 2.3+.
I see the msvcrt.setmode function. Is this my only path? Is it valid to
change the mode of stdout? The file.newlines is not writable.
|
by: Rouben Rostamian |
last post by:
Consider the following demo program:
#include <stdio.h>
int main(void)
{
unsigned char c;
for (c=0; c<15; c++)
putchar(c);
|
by: John Forkosh |
last post by:
I have a C program that writes binary output to stdout,
which works fine in Unix/Linux. But when someone else
compiled and ran it in Windows, every time the program
emitted a 0x0A, Windows interpreted it as an lf, and
preceded it with a spurious 0x0D cr. Cute. Is there
some way I can freopen() (so to speak) stdout in binary
mode under Windows so that doesn't happen? Hopefully,
the fix would be portable, so it could compile and...
|
by: Charles F McDevitt |
last post by:
I'm converting some old programs that use old iostreams.
In one program, the program is using cout to output to the stdout stream.
Part way through, the program wants to put some binary data out, and changes
the iostream to binary like this:
cout << "this is text" << eol;
binary(cout);
cout << "this is binary" << eol;
text(cout);
cout << "back to text mode" << eol;
| |
by: cedarson |
last post by:
I am having trouble writing a simple code that will convert an int (
487 for example ) to binary form just for the sake of printing the
binary. Can someone please help? Thanks!
|
by: Miro |
last post by:
VB 2003
at the end of the code, this works great.
bytCommand = Encoding.ASCII.GetBytes("testing hello send text")
udpClient.Send(bytCommand, bytCommand.Length)
and this recieves it
Dim strReturnData As String = _
System.Text.Encoding.ASCII.GetString(receiveBytes)
|
by: David Hirschfield |
last post by:
I have a pair of programs which trade python data back and forth by
pickling up lists of objects on one side (using
pickle.HIGHEST_PROTOCOL), and sending that data over a TCP socket
connection to the receiver, who unpickles the data and uses it.
So far this has been working fine, but I now need a way of separating
multiple chunks of pickled binary data in the stream being sent back and
forth.
Questions:
|
by: Bartc |
last post by:
The stdin/stdout files of C seem to be always in Text mode.
Is there any way of running a C program so that these (but especially
stdout) are in Binary mode instead?
(I'm in the process of wrapping a different language around C which doesn't
want the concept of text and binary files. But if I output a string such as
"ONE\nTWO\n", this will behave differently between stdout and a regular
(binary) file. Examples on my OS:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |