473,474 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to write binary data to stdout?

Consider the following demo program:

#include <stdio.h>

int main(void)
{
unsigned char c;

for (c=0; c<15; c++)
putchar(c);
return 0;
}

As is, the output of the program is platform-dependent.
For instance, in the UNIX environment, it puts 15 characters
to stdout. Under windows it puts out at least 16, because c=10
gets translated to newline+carriage return.

This brings me to:

Question: Is it possible to close stdout then reopen it in binary
mode in a platform-independent way?

If that were possible, then the binary-mode output of the program
would be the same on all platforms.

The purpose of my real program (not this demo,) is to act as a
filter from stdin to stdout, therefore fopen(filename, "wb") is
of not much help.

--
Rouben Rostamian <ro*******@umbc.edu>
Nov 13 '05 #1
4 25601
On 2003-10-18, Rouben Rostamian <ro****@pc18.math.umbc.edu> wrote:
Question: Is it possible to close stdout then reopen it in binary
mode in a platform-independent way?


No.

If you are seeking a completely standard solution, you are better off
outputting a text encoding of the binary data. If you are writing
programs intended to read each others input/output, the receiving
program would need to decode the stream.

-- James
Nov 13 '05 #2
ro****@pc18.math.umbc.edu (Rouben Rostamian) wrote:

<snip>
Question: Is it possible to close stdout then reopen it in binary
mode in a platform-independent way?
Well, there are two problems:

1) stdout need not be a modifiable lvalue, so you can't just
use fopen - you cannot come up with a suitable filename anyway.
To solve this, one could come up with
reopen( NULL, "wb", stdout);
running into problem number

2) It is implementation defined which (if any) and under what
circumstances mode changes are permitted.
If that were possible, then the binary-mode output of the program
would be the same on all platforms.
That would be great, but unfortunately it's impossible. stdin,
stdout and stderr are text streams per definition, and there is
no portable way to switch them to binary mode.
The purpose of my real program (not this demo,) is to act as a
filter from stdin to stdout, therefore fopen(filename, "wb") is
of not much help.


I cannot come up with a better solution, sorry.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #3
Rouben Rostamian wrote:

Consider the following demo program:

#include <stdio.h>

int main(void)
{
unsigned char c;
for (c=0; c<15; c++) putchar(c);
return 0;
}

As is, the output of the program is platform-dependent.
For instance, in the UNIX environment, it puts 15 characters
to stdout. Under windows it puts out at least 16, because c=10
gets translated to newline+carriage return.

This brings me to:

Question: Is it possible to close stdout then reopen it in binary
mode in a platform-independent way?

If that were possible, then the binary-mode output of the program
would be the same on all platforms.

The purpose of my real program (not this demo,) is to act as a
filter from stdin to stdout, therefore fopen(filename, "wb") is
of not much help.
This is handled by freopen.

if (freopen(NULL, "wb", stdout)) {...}

Note that it may fail, and has some implementation dependancies.From N869:


7.19.5.4 The freopen function

Synopsis
[#1]
#include <stdio.h>
FILE *freopen(const char * filename,
const char * mode,
FILE * restrict stream);

Description

[#2] The freopen function opens the file whose name is the
string pointed to by filename and associates the stream
pointed to by stream with it. The mode argument is used
just as in the fopen function.215)

____________________

215The primary use of the freopen function is to change the
file associated with a standard text stream (stderr,
stdin, or stdout), as those identifiers need not be
modifiable lvalues to which the value returned by the
fopen function may be assigned.
[#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.

[#4] The freopen function first attempts to close any file
that is associated with the specified stream. Failure to
close the file is ignored. The error and end-of-file
indicators for the stream are cleared.

Returns

[#5] The freopen function returns a null pointer if the open
operation fails. Otherwise, freopen returns the value of
stream.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #4
CBFalconer <cb********@yahoo.com> wrote:
Rouben Rostamian wrote:
Question: Is it possible to close stdout then reopen it in binary
mode in a platform-independent way?


This is handled by freopen.

if (freopen(NULL, "wb", stdout)) {...}

Note that it may fail, and has some implementation dependancies.
From N869:


One of which is that a null first argument to freopen() is a C99
feature.

Richard
Nov 13 '05 #5

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

Similar topics

3
by: kee | last post by:
Hi All, I am trying to write binary data to a file, which is bmp image: Open "d:\temp\test001.bmp" For Binary Access Write As #1 Put #1, 1, strImage Close #1 *** strImage contains binary...
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: Tom Van Ginneken | last post by:
Hi, I need to write binary data to a serial port. I am using this function: #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); I am able to write a alpha-numeric...
3
by: Billy Smith | last post by:
I'm trying to write a little utility that will write some binary data to a file via a javascript and Windows Script Host under Windows XP. The only way to do this that I can find is to convert...
0
by: lialie | last post by:
Hi~ I would like to save images in OLE field in Microsoft Access. It writes the binary data which read from an JPEG/BMP file. But seems I meet an encoding problem. The following code demos...
4
by: Swan | last post by:
Hi,I am sending binary data from VB to ASP through HttpSendRequest API.I am getting size of file on ASP(using Request.TotalBytes),which I am sending.Now I want to write it on specific path on...
0
by: Lambda | last post by:
I'd like to write compressed data to a file. To improve performance, I want to use buffer. I read some books, they all talk about 'character' stream. But I think compressed data should be...
5
by: Mars creature | last post by:
Hi guys, I am new to Python, and thinking about migrating to it from matlab as it is a really cool language. Right now, I am trying to figure out how to control read and write binary data, like...
1
by: Muhs | last post by:
Hi all.. I have converted text document to blob(binary large objects) in an array binaryArray , now the data needs to be saved in database table 'Dokumet' The following code does not save the...
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.