[vorbis] Getting started with vorbisfile_example.c (MS VC 6)

Craig Dickson crdic at pacbell.net
Thu Jun 27 13:24:17 PDT 2002


Alan MacDonald wrote:

> What makes it even more confusing is, what you are reading will mostly look
> correct, except the carriage returns and line feeds are translated to DOS
> mode with 'r'.  'rb' disables that translation.  Maybe it might be worth it
> to add a comment to the example files on how to use fopen in windows, even
> though the examples use stdio and stdout, since this seems to come up often.
>
> Something along the lines of, "If you re-implement this example using
> fopen() in a WIN32 environment make sure that you use "rb" or "wb" for your
> access mode to avoid carriage return and line feed translation"

Of course, using stdin and stdout doesn't evade the problem, it merely
hides it, because stdin and stdout are in text mode by default and will
mess up your Ogg stream just the same. You have to do this to fix it:

#ifdef _WIN32
#include <io.h>
#endif

int main(int argc, char** argv)
{
#ifdef _WIN32
   _setmode(_fileno(stdin), _O_BINARY);
   _setmode(_fileno(stdout), _O_BINARY);
#endif

  // etc.
}

There is probably some other preprocessor symbols for Win64, the old
16-bit Windows, and MS-DOS (if anyone cares).

All this is not really a "stupidity" of Microsoft OS's, unless you're
such a sad victim of Unix myopia that any OS that uses non-Unix line
endings (which is to say, almost all non-Unix-like OS's) seems "stupid".
In C, it has been standard at least since K&R 1st ed. (1978) to allow
"b" or "t" in the fopen mode parameter to specify text or binary file
access, and if you want to write code that is portable to non-Unix-like
systems, you need to use "b" or "t" even if your own OS of choice
doesn't require them. C libraries that actually don't accept "b" and "t"
are uncommon at this point, and non-standards-compliant. There is an
entry in the comp.lang.c FAQ dealing with this (very briefly).

While the C standard provides for "b" and "t", it does not, as far as I
know, provide for a standard way to change an already-open file from one
mode to the other, hence the Microsoft _setmode extension shown above.

Linefeed-only line endings are nice, and I wish they had been standard
from the beginning, rather than carriage return/linefeed pairs, which
are mildly more annoying to deal with. Then this would all be a
non-issue. But that's not how it is, and if we are to be good little
programmers, we have to accept that and deal with it.

Craig

-------------- next part --------------
A non-text attachment was scrubbed...
Name: part
Type: application/pgp-signature
Size: 190 bytes
Desc: not available
Url : http://lists.xiph.org/pipermail/vorbis/attachments/20020627/92799d56/part.pgp


More information about the Vorbis mailing list