Mail Archives: djgpp/2009/04/03/09:15:49
> From: "Rod Pemberton" <do_not_have AT nohavenot DOT cmm>
> Date: Thu, 2 Apr 2009 19:02:06 -0400
>
> setmode(fileno(stdout), O_BINARY);
>
> First, is there an equivalent to this in ANSI C?
No. ANSI C says that everything about stdin and stdout is
implementation-defined, IIRC.
> Second, shouldn't I be able to do the exactly same as the setmode() line
> with ANSI C's freopen()?
I don't think this is possible (but I didn't try hard enough, so I
could be wrong). Redirecting stdout explicitly affects file
descriptor 1 which was inherited from the parent shell, and `freopen'
closes that descriptor. The new FILE object reuses the same file
descriptor 1, but the redirection is lost when the original descriptor
is closed, because the redirection is maintained by the OS, which is
told that the file is closed.
> How do I get both redirection capability and binary output with stdout when
> using ANSI C functions such as freopen()?...
Why isn't using `setmode' good enough? That's what all applications
that need this do. Just be careful not to switch stdout to binary if
it is not redirected, since switching the console device to binary has
some unpleasant side effects in DJGPP: e.g., SIGINT is not produced by
Ctrl-C. So I'd suggest
if (!isatty (fileno (stdout)))
setmode (fileno (stdout), O_BINARY);
- Raw text -