Write binary data to stdout

Aug 25, 2012 at 12:30pm
When I use:

fwrite(caracter,sizeof (unsigned char), SizeF, stdout);

each NL char (0x0A) is replaced by a pair CR, NL (0x0D, 0x0A)

How avoid this behaviour?
There are others problems?

Thanks for any help.

Aug 25, 2012 at 1:04pm
You need to open your file in binary mode. The mapping of the NL chars occurs when files are opened in text mode.

Sorry, just registered that you're writing to stdout, which I guess assumes text mode.
Last edited on Aug 25, 2012 at 1:07pm
Aug 25, 2012 at 4:53pm
Not sure, but you could try:
freopen(NULL, "wb", stdout);
Last edited on Aug 25, 2012 at 4:53pm
Aug 25, 2012 at 5:51pm
Thanks but the program crash with exception Code 0xc0000417
Aug 25, 2012 at 7:10pm
Aug 26, 2012 at 12:08pm
Thanks, this work:

_setmode( _fileno( stdout ), _O_BINARY );
Aug 26, 2012 at 2:58pm
... or you can open the file in binary mode.
Aug 26, 2012 at 3:20pm
He did not open a file.
Aug 26, 2012 at 8:21pm
That's interesting. Then maybe someone needs to determine if it was opened in the wrong mode; and if the mode has to be changed, then maybe it should be changed back to text.
Aug 26, 2012 at 8:53pm
stdout is like cout in C++. It is automatically opened when the program starts.
Topic archived. No new replies allowed.