472,958 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 25387
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.