Article View: comp.os.msdos.programmer
Article #1522Re: Print to stdout in binary mode?
From: robert.k.nichols
Date: Wed, 16 Sep 1992 01:32
Date: Wed, 16 Sep 1992 01:32
47 lines
2227 bytes
2227 bytes
In article <BuLKyI.Jz8@news.cso.uiuc.edu> gordon@osiris.cso.uiuc.edu (John Gordon) writes:
>
> Hi. I am trying to make a C program write to stdout in binary mode,
>and can't figure out how to do it. The problem is that I am writing to a
>device which expects lines to be terminated by a linefeed, *not* an LF/CR
>pair. I would like to do this to stdout, to be able to pipe it. I suppose
>I could write to an intermediate file, if I had to, though. Can anyone
>figure out a way? I tried writing to "con", but that can't be redirected.
...
I fought this battle, too, and finally discovered that I had to make an
int21 call to force the driver into binary mode. Just using normal I/O
calls to set binary mode didn't work. Here's my code fragment:
int c,n;
FILE *prn; /* usually opened as device "PRN" in "wb" mode,
but might be stdout */
c = setmode(fileno(prn),O_BINARY); /* One would think that this
would suffice */
/* Ensure that the driver is REALLY in binary mode */
n = fileno(prn);
_asm {
mov bx,n
mov ax,4400h /* 4400: get device data */
int 21h
mov c,dx
}
if(!(c & 0x20)) { /* If not in binary mode (and it NEVER is) */
c = c & 0xff | 0x20; /* set the BINARY mode bit */
_asm {
mov bx,n
mov ax,4401h /* 4401: set device data */
mov dx,c
int 21h
}
}
Yes, I know that the code could have been simpler, but I was experimenting,
and once I found the magic combination I never bothered to clean it up.
(Why optimize code that executes once during a program's initialization?)
Bob Nichols
AT&T Bell Laboratories
rnichols@ihlpm.ih.att.com
Technical Details
Message-ID:
<1992Sep16.013220.10987@cbfsb.cb.att.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!wupost!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnewsc!cbfsb!cbnewsg.cb.att.com!rnicholsReferences:
<BuLKyI.Jz8@news.cso.uiuc.edu>Show Full Headers
Complete Article Headers
Newsgroups: comp.os.msdos.programmer Path: arc1-ca.pugleaf.net!sparky!uunet!wupost!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnewsc!cbfsb!cbnewsg.cb.att.com!rnichols From: rnichols@cbnewsg.cb.att.com (robert.k.nichols) Subject: Re: Print to stdout in binary mode? Message-ID: <1992Sep16.013220.10987@cbfsb.cb.att.com> Summary: Need to force the driver into binary mode via int21, func 4401h Keywords: stdout,binary Sender: news@cbfsb.cb.att.com Organization: AT&T References: <BuLKyI.Jz8@news.cso.uiuc.edu> Date: Wed, 16 Sep 1992 01:32:20 GMT Lines: 47