[wplug] C code problem
Henry Umansky
hmust2+ at pitt.edu
Wed Oct 23 17:51:01 EDT 2002
Ahhh I see, so all I had to do was flush the buffer like so:
include <stdio.h>
void main() {
long i;
for(i=0;i<100000000000;i++)
if(i%1000000==0) {
fprintf(stdout, "%d : %d\r", i, i);
fflush(stdout);
}
}
And it seemed to work like a charm. Thanks for the info. No this isn't a
homework assignment, its for personal knowledge, I'm trying to see how some
programs print to the same line with the output looking very smooth. My
output is very choppy when I don't have that modulo in there.
--On Wednesday, October 23, 2002 5:21 PM -0400 Rick Smith
<rick at rbsmith.com> wrote:r
> On Wed, Oct 23, 2002 at 05:05:38PM -0400, Rick Smith wrote:
>> On Wed, Oct 23, 2002 at 04:05:30PM -0400, Henry Umansky wrote:
>> > Can someone explain to me why on my machine the printout value for i
>> > is different. I compile this with gcc version 2.95.3 20010315. Can
>> > someone else try compiling this and running it and keep an eye on the
>> > two values that are printed out.
>>
>> I can answer this but want to make sure it is not a homework
>> assignment or something that is intended for you to figure
>> out on your own. I'll give you a hint: it has nothing to
>> do with the compiler. I'll give you another hint: look
>> at the first line.
>
> Hmm, that didn't read quite what it was like when it was in
> my head. Reads like I'm thinking you were being devious.
> Sorry about that.
>
> stdout is buffered, stderr is not.
> To see the difference fprintf(stderr, "%d : %d\r", i, i)
>
> buffer is flushed when it is full (and maybe some other events,
> like timeout). Because you do \r and overwrite, all you see
> is the end of the buffer, which randomly may only update
> the first number, with the second number being at the beginning
> of the next buffered write.
>
> -- Rick
> _______________________________________________
> wplug mailing list
> wplug at wplug.org
> http://www.wplug.org/mailman/listinfo/wplug
Henry Umansky
University of Pittsburgh
Systems/Programmer III
www.pitt.edu/~hmust2
hmust2 at pitt.edu
(412)624-4357
More information about the wplug
mailing list