Mail Archives: djgpp/1993/06/08/13:58:58
> I would like to throw a question out to the masses:
>
> binary file i/o.
>
> Anyone had any trouble with it? Have I just plain done something brain
> damaged? or is it under the heading of 'vagaries of messy-dos'? Specifics
> to those interested/knowledgeable in this are, summaries to the list...
A. When you call fopen(), be sure to set the second parameter to "rb",
"wb", or whatever. Just make sure there is a "b" in there. This
will also work on UNIX without modification. For example:
fopen("binary.fil", "rb");
B. When you call open(), binary-or O_BINARY into the second parameter.
If the code will also be used on UNIX, it is common to define
O_BINARY if it does not already exist. For example:
#include <fcntl.h>
#ifndef O_BINARY
#define O_BINARY 0
#endif
open("binary.fil", O_RDONLY | O_BINARY);
C. By default, stdin, stdout, and stderr are in text mode. You need
to call setmode() to fix them. As I recall, you need to include
<fcntl.h> to get O_BINARY. If the code is to be used on UNIX, you
should surround this with "#ifdef __MSDOS__". For example:
#include <fcntl.h>
#ifdef __MSDOS__
setmode(fileno(stdin), O_BINARY);
setmode(fileno(stdout), O_BINARY);
#endif
D. If you switch a file between text and binary or binary and text
mode, and you are using stdio on that file, you should flush the
buffers to that file before switching.
If you follow these rules, everything should work fine.
--
Eric Backus
ericb AT lsid DOT hp DOT com
(206) 335-2495
- Raw text -