B
Ben Bacarisse
Bartc said:C's text mode is already giving me problems by being too clever:
I'm using a language superimposed on C's runtime, which generates CR,LF for
new lines. This seems to get translated, when writing to STDOUT, to
CR,CR,LF, which on the screen looks the same. But sent to a file (using
Windows' >), these lines cause my (15yo) editor to crash.
It is not C being too clever, it the programmer not being clever
enough. If, in Windows, you are writing \r\n to a file, then one can
assume that you don't want to treat it as test file. If you do, it
will go wrong. You have two options:
(1) Treat your output as a binary stream. The downside is that you
will be putting \r\n into files even when your program is ported to a
system that prefers not to have these as line endings.
(2) If your output is text, treat it as text. Write the output in the
portable way C expects you to and the output will have the correct
line endings. The advantage you get is a simpler program that is
portable to other systems.
If your program is intended to generate output for use on "other"
systems, then (1) is the only way to go (and you need some way to tell
you program what sort of line ending to write). You will also not be
able to write to stdout[1]. If you are writing text files for use
"here" then (2) is the way to go.
Some programs need to do both, and then they have to know the mode of
each stream and the target line ending they must use for those stream
that are binary. Taking that a bit further, you might also need to
know the character encoding that the foreign system expects. Such
programs were quite common when truly diverse systems were more common.
Writing \r\n to a text stream is very unlikely to be the right thing
to do. Equally, not using text mode when writing text for the local
system just sounds like cutting your nose off to spite your face.
[1] There is often a system-specific way to re-open stdout in the mode
of your choice, so this is not a hard and fast rule.