delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/11/02/11:06:24

Date: Thu, 2 Nov 1995 17:39:44 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Justen Keven Marshall <jkmarsha AT socs DOT uts DOT edu DOT au>
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Re: CR/LF pairs... NO NO NO!

On 2 Nov 1995, Justen Keven Marshall wrote:

>   I am at my wits end trying to print a line-feed
>   without a carriage return. (Or the other way around.)
>   What happens is that when I try to print char(10) in
>   a file, with printf redirected, fprintf, cout, putchar,
>   fputchar, or whatever, it puts BOTH not just one.

DOS C compilers treat text and binary files differently.  When writing to 
a text file, a CR is produced for every LF, so that you could port 
programs which print like this:

	printf("Hello, world!\n");

and expect a CR-LF pair to be written, as required for DOS text files.

Any given file is treated as text or binary according to how it was 
open.  The default is TEXT, so that if you say `fp = fopen("file", "w")', 
it is open as a text file.  This is again for portability reasons, 
because Unix programs don't know about the difference.  To open a file in 
binary mode, just add "b" to the flags you pass to fopen(), like this:

	FILE *fp = fopen("file", "wb");

or use O_BINARY, if you do the same with open().

If you want to switch an already open file to binary mode (like for stdin 
and stdout, which are open before your main() function runs), use the 
setmode() library function, like this:

	setmode(fileno(stdout), O_BINARY);

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019