Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #163729

Re: ot: on stdin, stdout, stderr and crlf on windows

From John Forkosh <forkosh@panix.com>
Newsgroups comp.lang.c
Subject Re: ot: on stdin, stdout, stderr and crlf on windows
Date 2021-12-07 01:11 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <somcbq$frv$1@reader1.panix.com> (permalink)
References (1 earlier) <soiheo$jo9$1@dont-email.me> <sokh4l$8l2$1@reader1.panix.com> <87zgpem34j.fsf@bsb.me.uk> <soku6k$oo4$1@dont-email.me> <87o85tnc8v.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> Bonita Montero <Bonita.Montero@gmail.com> writes:
> 
>> Am 06.12.2021 um 11:36 schrieb Ben Bacarisse:
>>> John Forkosh <forkosh@panix.com> writes:
>>>
>>>> Bonita Montero <Bonita.Montero@gmail.com> wrote:
>>>>> Meredith Montgomery wrote:
>>>>>>
>>>>>> Windows provides me _setmode(), with which I can put stdout on
>>>>>> _O_BINARY.  It then stops replacing \n with \r\n.  However, it seems
>>>>>> that Windows does not do any such replacements on stdin and stderr.  I
>>>>>> looked for a confirmation of this apparent fact and could not find it.
>>>>   <<snip>>
>>>>>
>>>>> Check the output of this in your hex-editor:
>>>>>
>>>>> #include <stdio.h>
>>>>> #include <fcntl.h>
>>>>> #include <io.h>
>>>>> #include <stdlib.h>
>>>>>
>>>>> int main( int argc, char **argv )
>>>>> {
>>>>>    if( argc >= 2 && _setmode( _fileno( stderr ), _O_BINARY ) == -1 )
>>>>>            return EXIT_FAILURE;
>>>>>    fprintf( stderr, "it works !\n" );
>>>>> }
>>>>>
>>>>> Without a commandline-parameter you've 0x0D, 0x0A
>>>>> as a line-separator, with you've got only 0x0A.
>>>
>>> Summary: making stdout binary.
>>>
>>>> Yeah, that's more or less what I've been doing, but is there
>>>> maybe a portable way to do it, i.e., for programs intended to
>>>> run both under windows and linux?
>>>
>>> There's a standard function that /could/ do it, but it is allowed to be
>>> no-op.  I don't have access to a Windows programming environment, so I
>>> can't check it:
>>>
>>>    freopen(NULL, "ab", stdout);
>>>
>>> (or maybe "wb", it's the b you want of course).
>> Doesn't work with either wb or ab:
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> int main( int argc, char **argv )
>> {
>>       if( argc < 2 )
>>               return EXIT_FAILURE;
>>       FILE *pfErr = freopen( NULL, argv[1], stderr);
>>       printf( "%p", (void *)pfErr );
>> }
>>
>>  en.cppreference.com says:
>>
>> "Microsoft CRT version of std::freopen does not support any mode
>>  changes when filename is a null pointer and treats this as an
>>  error (see documentation). A possible workaround is the non
>>  -standard function _setmode()."
> 
> I feared this would be the case.  It was many years ago when this last
> came up.  Maybe some alternative C library has made what appears to be
> the trivial modification to make it work?
> 
> Anyway, if the most widely used C library does not support it, it's not
> portable in any useful way.

Thanks, Ben and Bonita (and Malcom and Manfred), yeah I also "feared
this would be the case", i.e., no portable solution like freopen(),
which would be syntactically identical on all platforms and "just work",
whether needed (windows) or not (linux).

By the way, at least at the time when I wrote that stackexchange post,
some windows C compilers used _setmode(_fileno(stderr),_O_BINARY)
like Bonita said, but some others used setmode(fileno(stderr),O_BINARY).
So additional #ifdefs needed to resolve that. And I'm also not sure
that _MSC_VER always identifies all windows C compilers. The elaborate
#ifdefs illustrated on that stackexchange post were what I ended up
with to accommodate all the variations I'd encountered at the time
I wrote it. So I was hoping to find some less elaborate and fewer
lines of gobbledy-gook code, if not entirely 100% portable, to identify
and handle all possible windows situations.
-- 
John Forkosh  ( mailto:  j@f.com  where j=john and f=forkosh )

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

ot: on stdin, stdout, stderr and crlf on windows Meredith Montgomery <mmontgomery@levado.to> - 2021-12-04 20:24 -0300
  Re: ot: on stdin, stdout, stderr and crlf on windows Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2021-12-05 00:01 +0000
  Re: ot: on stdin, stdout, stderr and crlf on windows Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2021-12-05 00:13 +0000
    Re: ot: on stdin, stdout, stderr and crlf on windows Meredith Montgomery <mmontgomery@levado.to> - 2021-12-04 23:09 -0300
      Re: ot: on stdin, stdout, stderr and crlf on windows Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-04 18:27 -0800
      Re: ot: on stdin, stdout, stderr and crlf on windows Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2021-12-05 03:33 +0000
        Re: ot: on stdin, stdout, stderr and crlf on windows Meredith Montgomery <mmontgomery@levado.to> - 2021-12-05 09:17 -0300
  Re: ot: on stdin, stdout, stderr and crlf on windows Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-05 15:04 +0100
  Re: ot: on stdin, stdout, stderr and crlf on windows Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-05 15:13 +0100
    Re: ot: on stdin, stdout, stderr and crlf on windows John Forkosh <forkosh@panix.com> - 2021-12-06 08:20 +0000
      Re: ot: on stdin, stdout, stderr and crlf on windows Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-06 09:45 +0100
      Re: ot: on stdin, stdout, stderr and crlf on windows Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-06 10:36 +0000
        Re: ot: on stdin, stdout, stderr and crlf on windows Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-06 13:03 +0100
          Re: ot: on stdin, stdout, stderr and crlf on windows Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-06 12:34 +0000
            Re: ot: on stdin, stdout, stderr and crlf on windows Manfred <noname@add.invalid> - 2021-12-06 14:38 +0100
              Re: ot: on stdin, stdout, stderr and crlf on windows Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-12-06 06:00 -0800
                Re: ot: on stdin, stdout, stderr and crlf on windows Manfred <noname@add.invalid> - 2021-12-06 18:59 +0100
              Re: ot: on stdin, stdout, stderr and crlf on windows Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-06 17:08 +0000
                Re: ot: on stdin, stdout, stderr and crlf on windows scott@slp53.sl.home (Scott Lurndal) - 2021-12-06 17:18 +0000
            Re: ot: on stdin, stdout, stderr and crlf on windows John Forkosh <forkosh@panix.com> - 2021-12-07 01:11 +0000
              Re: ot: on stdin, stdout, stderr and crlf on windows Bart <bc@freeuk.com> - 2021-12-07 09:56 +0000
                Re: ot: on stdin, stdout, stderr and crlf on windows John Forkosh <forkosh@panix.com> - 2021-12-08 08:13 +0000
  Re: ot: on stdin, stdout, stderr and crlf on windows Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-12-10 01:10 -0800
    Re: ot: on stdin, stdout, stderr and crlf on windows gazelle@shell.xmission.com (Kenny McCormack) - 2021-12-10 11:00 +0000

csiph-web