13

On Windows this

#include <stdio.h>

int main() { 
    putc('A',stdout);
    putc('\r',stdout); 
    putc('\n',stdout);
}

outputs

A<CR><CR><LF>

How to write just LF char to stdout without automatic conversion to CR LF?

I need it to make simple socket stream reader to stdout. I've tried bcc32 from CodeGear, mingw, tinycc all yield same result, changing putc to putchar, fputc, fwrite doesn't help either.

Daniel Leung
  • 165
  • 1
  • 6

3 Answers3

14

The MSVC solution is:

#include <io.h>
#include <fcntl.h>
...
_setmode(1,_O_BINARY)

Other runtimes may provide the C99 solution or an alternate way. EDIT: I believe setmode([file number],O_BINARY) originated on Borland Turbo C, and other compilers for MS-DOS and Windows imitated it. The _ prefix is done to keep the namespace clean, and may not be present on some compilers.

Random832
  • 37,415
  • 3
  • 44
  • 63
  • But it doesn't support passing `NULL` as the filename, and the stdout you start with doesn't necessarily _have_ a filename. – Random832 Apr 28 '11 at 04:01
  • Thanks. It works, even without io.h included on tinycc and mingw. on bcc32 io.h must be included then change _setmode to setmode. – Daniel Leung Apr 28 '11 at 05:03
  • Starting with Windows 10 (at least) this is insufficient. The console does its own translation, which should be disabled separately by setting `DISABLE_NEWLINE_AUTO_RETURN` console mode. (The constant is defined in Windows 10 SDK, but not in Windows 8 SDK) – AnT stands with Russia Jan 24 '18 at 09:15
  • @AnT The OP does not mention the console. I assumed stdout was a pipe or file, because it's impossible to tell the difference between one CR and two in a row as in the example on the console. – Random832 Jan 24 '18 at 15:27
3
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#endif

#ifdef __BORLANDC__
#define _setmode setmode
#endif

#include <stdio.h>

static void binary_stdout(void) {
#ifdef _WIN32
    _setmode(_fileno(stdout), _O_BINARY);
#endif
}

int main(void) {
    binary_stdout();
    printf("\n");
    return 0;
}
Lassi
  • 3,522
  • 3
  • 22
  • 34
3

A text file converts the C character '\n' into the native line ending on output, and converts the native line ending on input into a single '\n'.

To get the result you require, you'd have to change stdout into a binary file stream.

A partial answer is found here. If you have a C99-compliant library, using:

if (freopen(0, "wb", stdout) == 0)
    ...oops...operation failed...

will attempt to change standard output to a binary stream. However, on Windows, the 'C99-compliant library' might be a problem. Nominally, this is the portable (because standard) answer. There is likely a Windows-specific function to do the same job.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • @Ignacio: yes, thanks - found that out...we live, we learn, we answer questions. – Jonathan Leffler Apr 28 '11 at 03:44
  • @James: The `freopen()` function has been in `` since Version 7 Unix, but the behaviour with a null pointer for the file name is new in C99. The C89 standard says nothing about a null pointer. – Jonathan Leffler Apr 28 '11 at 04:10