carriage return with stdout and stderr

View: New views
4 Messages — Rating Filter:   Alert me  

carriage return with stdout and stderr

by Alexander Best :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

i'm running something similar to this pseudo-code in an app of mine:

for (i=0 ....)
    fprintf(stdout,"TEXT %d\r", int);

what's really strange is that if i print to stdout the output isn't very
clean. the cursor jumps randomly within the output (being 1 line). if i print
to stderr however the output looks really nice. the cursor says right at the
front of the output all the time. just like in burncd e.g.

what's causing this? because i'd rather print to stdout.

alex
_______________________________________________
freebsd-hackers@... mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..."

Re: carriage return with stdout and stderr

by Christoph Mallon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Alexander Best schrieb:

> i'm running something similar to this pseudo-code in an app of mine:
>
> for (i=0 ....)
>     fprintf(stdout,"TEXT %d\r", int);
>
> what's really strange is that if i print to stdout the output isn't very
> clean. the cursor jumps randomly within the output (being 1 line). if i print
> to stderr however the output looks really nice. the cursor says right at the
> front of the output all the time. just like in burncd e.g.
>
> what's causing this? because i'd rather print to stdout.

stdout is buffered, stderr is not. Try fflush().

        Christoph
_______________________________________________
freebsd-hackers@... mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..."

Re: carriage return with stdout and stderr

by Jilles Tjoelker :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, Jul 05, 2009 at 01:42:01PM +0200, Alexander Best wrote:
> i'm running something similar to this pseudo-code in an app of mine:

> for (i=0 ....)
>     fprintf(stdout,"TEXT %d\r", int);

> what's really strange is that if i print to stdout the output isn't very
> clean. the cursor jumps randomly within the output (being 1 line). if i print
> to stderr however the output looks really nice. the cursor says right at the
> front of the output all the time. just like in burncd e.g.

> what's causing this? because i'd rather print to stdout.

If you are writing to a terminal, stdout is line-buffered. This means
that output is flushed when the buffer is full or a '\n' is written. A
'\r' is not good enough. You can force a write using fflush(stdout).

stderr is always unbuffered, so everything is written immediately.

--
Jilles Tjoelker
_______________________________________________
freebsd-hackers@... mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..."

Re: carriage return with stdout and stderr

by Alexander Best :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

thanks. i remembered fprintf being buffered, but i always thought \r would
also empty the buffer. now that explains everything. ;-)

alex

Jilles Tjoelker schrieb am 2009-07-05:
> On Sun, Jul 05, 2009 at 01:42:01PM +0200, Alexander Best wrote:
> > i'm running something similar to this pseudo-code in an app of
> > mine:

> > for (i=0 ....)
> >     fprintf(stdout,"TEXT %d\r", int);

> > what's really strange is that if i print to stdout the output isn't
> > very
> > clean. the cursor jumps randomly within the output (being 1 line).
> > if i print
> > to stderr however the output looks really nice. the cursor says
> > right at the
> > front of the output all the time. just like in burncd e.g.

> > what's causing this? because i'd rather print to stdout.

> If you are writing to a terminal, stdout is line-buffered. This means
> that output is flushed when the buffer is full or a '\n' is written.
> A
> '\r' is not good enough. You can force a write using fflush(stdout).

> stderr is always unbuffered, so everything is written immediately.

_______________________________________________
freebsd-hackers@... mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..."