On Thu, 4 Apr 2002, Nadav Har'El wrote:

> On Thu, Apr 04, 2002, Omer Musaev wrote about "RE: c question":
> > Instead of using buffered output and flushing it on every char,
> > you can use raw output.
> >..
> > ---------------------------------------------------
> >        write(1, "#", 1 ) ; /* man write(2) */
> >        sleep( 1 ) ;
>
> This is getting into deep C territory, but...
> You don't have to switch to system calls and lose the convenience of printf
> in order not to have your output buffered.

And mixing stdio.h output with system calls output can lead to all kind of
weird side effects:

/*****************/
/* compile with gcc test.c and run ./a.out */
#include <stdio.h>
#include <unistd.h>

int main()
{
    printf("%s", "Hello ");
    write(1, "World!\n", 7);
    printf("\n");
    return 0;
}
/*******************/

When I worked in Smart-Link and asked a more experienced fellow programmer
how I can, in the Win32 RTL, convert an open() filehandle to its Windows
HFILE equivalent, he told me that mixing IO for two different kinds of
handles is usually a bad idea. Then he told me about the stdio.h/open()
side-effects.

I remember that for a long time, until I started to seriously program on
UNIX, I did not know where everything in the libraries included with MSVC
belongs and where I should use what. There are just so many ways to
accomplish any certain task there, that it can confuse one senselessly.
"There are too many ways to do it." My code was a mixture of MFC, pure
Win32 API calls and libc calls.

> You have two other alternatives:
>
> 1. fprintf into stderr instead of stdout. stderr by default is not
>    buffered.
>
> 2. Use setvbuf (see setbuf(3)) to make stdout unbuffered.
>    By default, stdout is line-buffered if the standard output is a terminal
>    (see isatty(3)) and block-buffered otherwise.
>

Nice. I did not know about them, although if you would have asked me, I
said they did exist.

> --
> Nadav Har'El                        |      Thursday, Apr 4 2002, 22 Nisan 5762
> [EMAIL PROTECTED]             |-----------------------------------------
> Phone: +972-53-245868, ICQ 13349191 |In Fortran, God is real unless declared
> http://nadav.harel.org.il           |an integer.
>
> =================================================================
> To unsubscribe, send mail to [EMAIL PROTECTED] with
> the word "unsubscribe" in the message body, e.g., run the command
> echo unsubscribe | mail [EMAIL PROTECTED]
>



----------------------------------------------------------------------
Shlomi Fish        [EMAIL PROTECTED]
Home Page:         http://t2.technion.ac.il/~shlomif/
Home E-mail:       [EMAIL PROTECTED]

"Let's suppose you have a table with 2^n cups..."
"Wait a second - is n a natural number?"


=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to