473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

binary output to stdout in Windows

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
execute under Unix, too, with no ill effects.
Thanks,
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
Nov 14 '05 #1
8 6056
try _setmode()

Search _setmode (third hit) in

http://msdn.microsoft.com/library

with respect,
Toni Uusitalo

"John Forkosh" <jo**@SeeSigForAddress.invalid.com> wrote in message
news:c0**********@reader2.panix.com...
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
execute under Unix, too, with no ill effects.
Thanks,
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )

Nov 14 '05 #2
In article <news:c0**********@reader2.panix.com>
John Forkosh <jo**@SeeSigForAddress.invalid.com> writes:
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
execute under Unix, too, with no ill effects.


In C99 there is a way (I think -- it is not in my draft; I should
probably buy the actual C99 .pdf file):

FILE *result;
...
result = freopen(NULL, "wb", stdout);

Here, "result" should compare equal to stdout on success, and NULL
on failure.

Unfortunately, passing NULL to freopen() in C89 results in undefined
behavior -- and in many real implementations, a crash and/or
core-dump.

You might use the theoretically-portable freopen() method as your
"standard" technique that is rarely used today but more frequently
tomorrow, and meanwhile resort to some nonstandard method(s) for
existing implementations. (I have no idea what those might be
beyond something spelled something like "setmode".)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #3
Chris Torek <no****@torek.net> wrote:
John Forkosh <jo**@SeeSigForAddress.invalid.com> writes:
<snip>
[How to transparently write binary data to stdout.]
In C99 there is a way (I think -- it is not in my draft; I should
probably buy the actual C99 .pdf file):

FILE *result;
...
result = freopen(NULL, "wb", stdout);

Here, "result" should compare equal to stdout on success, and NULL
on failure.

Unfortunately, passing NULL to freopen() in C89 results in undefined
behavior -- and in many real implementations, a crash and/or
core-dump.


Unfortunately, even in C99 this is not as portable as
one might think (or wish):

C99 7.19.5.4#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.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^

FWIW, on at least one popular platform this C99 program:

#include <stdio.h>
int main( void )
{
FILE *fp = freopen( NULL, "wb", stdout );
fputs( fp ? "Ok.\n" : "Dang!\n", stderr );
return 0;
}

prints 'Dang!' and leaves stdout closed! :-(

Conclusion: the only portable way (I can think of)
to write binary data from a C program is writing it
to a file explicitly fopen()ed in "[w|a][+]b" mode.

<snip>

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4

"Chris Torek" <no****@torek.net> wrote in message
news:c0*********@enews3.newsguy.com...
In article <news:c0**********@reader2.panix.com>
John Forkosh <jo**@SeeSigForAddress.invalid.com> writes:
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
execute under Unix, too, with no ill effects.
You might use the theoretically-portable freopen() method as your
"standard" technique that is rarely used today but more frequently
tomorrow, and meanwhile resort to some nonstandard method(s) for
existing implementations. (I have no idea what those might be
beyond something spelled something like "setmode".)
--


I didn't know freopen could be used to change file mode in the newer
standard.
But I guess this doesn't help the OP in the present time. Luckily all
programmers
love to write tons of #ifdefs ;-)

I'll use "theoretically-portable" phrase next to "theoretical performance"
numbers next time I advertise some piece of s**tware I've written ;-)

with respect,
Toni Uusitalo

Nov 14 '05 #5
Toni Uusitalo <to**************@pandot.nu> wrote:
: "John Forkosh" wrote:
: > 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
: > execute under Unix, too, with no ill effects. Thanks,

: try _setmode()
: Search _setmode (third hit) in
: http://msdn.microsoft.com/library
: --
: Toni Uusitalo

Thanks, Toni. As suggested, I modified code to include,
essentially,
if ( _setmode(_fileno(stdout), _O_BINARY)
== -1 ) ; /* error */
guarded by an ugly #ifdef to determine whether or not
it's a Windows compilation.
Mailed fix to user who replied that he hasn't had time
to try it yet, but is "sure it will work" -- poor, naive soul!
I'll post another short followup when outcome is known,
especially if my cynical, Murphy's Law attitude bears its
usual fruits.

Chris Torek wrote:
: In C99 there is a way (I think -- it is not in my draft; I should
: probably buy the actual C99 .pdf file):
: FILE *result;
: result = freopen(NULL, "wb", stdout);
: Here, "result" should compare equal to stdout on success, and NULL
: on failure. Unfortunately, passing NULL to freopen() in C89 results
: in undefined behavior -- and in many real implementations, a crash
: and/or core-dump.
: You might use the theoretically-portable freopen() method as your
: "standard" technique that is rarely used today but more frequently
: tomorrow, and meanwhile resort to some nonstandard method(s) for
: existing implementations. (I have no idea what those might be
: beyond something spelled something like "setmode".)
: --
: Chris Torek, Wind River Systems, http://web.torek.net/torek/index.html

-- and --

Irrwahn Grausewitz <ir*******@freenet.de> wrote:
: Unfortunately, even in C99 this is not as portable as
: one might think (or wish):
: C99 7.19.5.4#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.
: ^^^^^^^^^^^
: FWIW, on at least one popular platform this C99 program:
: #include <stdio.h>
: int main( void )
: { FILE *fp = freopen( NULL, "wb", stdout );
: fputs( fp ? "Ok.\n" : "Dang!\n", stderr );
: return 0; }
: prints 'Dang!' and leaves stdout closed! :-(
: Conclusion: the only portable way (I can think of)
: to write binary data from a C program is writing it
: to a file explicitly fopen()ed in "[w|a][+]b" mode.
: --
: Irrwahn Grausewitz (ir*******@freenet.de)

Thanks Chris and Irrwahn. For the time being, at least,
I used Toni's suggestion since a non-portable solution
is better than a portable non-solution.
Can't write to a file because program is used as a
cgi, and therefore has to emit bytes directly to stdout
so Apache (or whatever) can pick them up.

Thanks again for help, guys,
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
Nov 14 '05 #6
In <ac********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
FWIW, on at least one popular platform this C99 program:

#include <stdio.h>
int main( void )
{
FILE *fp = freopen( NULL, "wb", stdout );
fputs( fp ? "Ok.\n" : "Dang!\n", stderr );
return 0;
}

prints 'Dang!' and leaves stdout closed! :-(
If this platform doesn't claim C99-conformance, the program invokes
undefined behaviour, anyway. As Chris mentioned, this is not a C89
feature.
Conclusion: the only portable way (I can think of)
to write binary data from a C program is writing it
to a file explicitly fopen()ed in "[w|a][+]b" mode.


What is preventing a conforming implementation from having an fopen()
that fails for all these modes?

Being a QoI issue, it is reasonable to expect conforming C99
implementations to allow mode changes when freopen() is invoked this way.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
In <c0*********@enews3.newsguy.com> Chris Torek <no****@torek.net> writes:
In C99 there is a way (I think -- it is not in my draft; I should
probably buy the actual C99 .pdf file):


For this particular purpose, it is enough to upgrade to the last public
draft, N869:

[#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.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
John Forkosh <jo**@SeeSigForAddress.invalid.com> wrote:
: Toni Uusitalo <to**************@pandot.nu> wrote:
: : "John Forkosh" wrote:
: : > 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
: : > execute under Unix, too, with no ill effects. Thanks,

: : try _setmode()
: : Search _setmode (third hit) in
: : http://msdn.microsoft.com/library
: : --
: : Toni Uusitalo

: Thanks, Toni. As suggested, I modified code to include,
: essentially,
: if ( _setmode(_fileno(stdout), _O_BINARY)
: == -1 ) ; /* error */
: guarded by an ugly #ifdef to determine whether or not
: it's a Windows compilation.
: Mailed fix to user who replied that he hasn't had time
: to try it yet, but is "sure it will work" -- poor, naive soul!
: I'll post another short followup when outcome is known,

User reports fix works exactly as intended.
Thanks again,
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
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?...
7
by: Bix | last post by:
As this is my very first post, I'd like to give thanks to all who support this with their help. Hopefully, this question hasn't been answered (too many times) before... If anyone could explain...
5
by: gof | last post by:
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...
4
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);
5
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,...
40
by: rayw | last post by:
I've been trying to write a program that converts an entered number into its binary representation using three different techniques - bit shifting, mod 2, and itoa.. However, I'm getting 'snow...
4
by: Barry | last post by:
Hi, guys Basiclly, it is automated testing system. There is a main python script that handles the testing campagin. This main script will call another script that will in turn runs a few...
24
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...
27
by: CarlosMB | last post by:
Hello, I am writing code that uses a DLL which is supposed to print to console some useful information but for some reason it is not doing so. The environment is a bit complex to explain but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.