QNX and printf

Hi all,
I have what may be a rudimentary question, but I just don’t get it.
I made a little program, it has one argument, an integer.
I take the number (atoi(argv[1]) and use that in a for loop. for each
decrment from the number, I print the line “time remaining: [the
number]” I do this by printing "time remaining: " then for each
decrement printing to backspaces “\b\b” then writing the current number.

I never thought I’d say this, but it works fine in windows, it just
doesn’t work at all in QNX. nothing prints to the screen until the end
of the program

here is the code for windows
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>

int main(int argc, char* argv[])
{
if(argc > 1)
{
int time = atoi(argv[1]);
printf(“time remaining: “);
for(int i = time; i > 0; i–)
{
if(i < 10) printf(”\b\b %i”, i);
else printf("\b\b%i", i);
_sleep(1000);
}
printf("\b\b 0\n");
}
return 0;
}
it works fine, just the way I want it to.
but the code in QNX,

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
if(argc > 1)
{
int time = atoi(argv[1]);
printf(“time remaining: “);
for(int i = time; i > 0; i–)
{
if(i < 10) printf(”\b\b %i\c”, i);
else printf("\b\b%i\c", i);
sleep(1);
}
printf(“time remaining: 0\n”);
}
return 0;
}
doesn’t print anything until the last line “time remaining: 0”


Chris Nasr
cnasr[at]mechtronix[dot]ca
Mechtronix Systems Inc.

Hi,

Your output is being buffered since you aren’t sending newlines.
So… put a call to flush standard output after you print your line updates
before you sleep…

#include <stdio.h
#include <stdlib.h
#include <unistd.h

int main(int argc, char* argv[])
{
if(argc > 1)
{
int time = atoi(argv[1]);
printf(“time remaining: “);
for(int i = time; i > 0; i–)
{
if(i < 10) printf(”\b\b %i\c”, i);
else printf("\b\b%i\c", i);

Before the sleep, here, add:

fflush(stdout);

sleep(1);
}
printf(“time remaining: 0\n”);
}
return 0;
}
doesn’t print anything until the last line “time remaining: 0”


Chris Nasr
cnasr[at]mechtronix[dot]ca
Mechtronix Systems Inc.


F. Carl Sherbino

Senior Developer
Crisys Limited
8901 Woodbine Ave, Suite 110
Markham, Ontario
Canada
L3R 9Y4

Vox: (905) 474-9111 Ext. 32
Fax: (905) 474-0536

Adding a newline to each printf and removing the fflush will in fact update
every printf, but with the backspaces it don’t look so purdy :wink:

“Bill Caroselli @ Q-TPS” wrote:

I believe that output to stdout gets buffered by printf() regardless of how
many newlines there may or may not be.

“F. Carl Sherbino” <> csherbino@crisys.com> > wrote in message
news:> 3AEEDFC5.D2EE19A4@crisys.com> …
Hi,

Your output is being buffered since you aren’t sending newlines.


F. Carl Sherbino

Senior Developer
Crisys Limited
8901 Woodbine Ave, Suite 110
Markham, Ontario
Canada
L3R 9Y4

Vox: (905) 474-9111 Ext. 32
Fax: (905) 474-0536

I believe that output to stdout gets buffered by printf() regardless of how
many newlines there may or may not be.

“F. Carl Sherbino” <csherbino@crisys.com> wrote in message
news:3AEEDFC5.D2EE19A4@crisys.com

Hi,

Your output is being buffered since you aren’t sending newlines.

F. Carl Sherbino <csherbino@crisys.com> wrote:

Adding a newline to each printf and removing the fflush will in fact update
every printf, but with the backspaces it don’t look so purdy > :wink:

The “ANSI” function is “setvbuf()”, add:

setvbuf(stdout, NULL, _IONBF, 0);

at begining of the program, will stop line buffering on stdout.

-xtang

“Bill Caroselli @ Q-TPS” wrote:

I believe that output to stdout gets buffered by printf() regardless of how
many newlines there may or may not be.

“F. Carl Sherbino” <> csherbino@crisys.com> > wrote in message
news:> 3AEEDFC5.D2EE19A4@crisys.com> …
Hi,

Your output is being buffered since you aren’t sending newlines.


F. Carl Sherbino

Senior Developer
Crisys Limited
8901 Woodbine Ave, Suite 110
Markham, Ontario
Canada
L3R 9Y4

Vox: (905) 474-9111 Ext. 32
Fax: (905) 474-0536

Thanks so much, that did the trick. Much appreciated.

“F. Carl Sherbino” wrote:

Hi,

Your output is being buffered since you aren’t sending newlines.
So… put a call to flush standard output after you print your line updates
before you sleep…


#include <stdio.h
#include <stdlib.h
#include <unistd.h

int main(int argc, char* argv[])
{
if(argc > 1)
{
int time = atoi(argv[1]);
printf(“time remaining: “);
for(int i = time; i > 0; i–)
{
if(i < 10) printf(”\b\b %i\c”, i);
else printf("\b\b%i\c", i);

Before the sleep, here, add:

fflush(stdout);


sleep(1);
}
printf(“time remaining: 0\n”);
}
return 0;
}
doesn’t print anything until the last line “time remaining: 0”


Chris Nasr
cnasr[at]mechtronix[dot]ca
Mechtronix Systems Inc.


F. Carl Sherbino

Senior Developer
Crisys Limited
8901 Woodbine Ave, Suite 110
Markham, Ontario
Canada
L3R 9Y4

Vox: (905) 474-9111 Ext. 32
Fax: (905) 474-0536


Chris Nasr
cnasr[at]mechtronix[dot]ca
Mechtronix Systems Inc.

Hmm. Does this thread indicate that the Microsoft implementation of printf is
unbuffered by default? Is this POSIX compliant?

Chris Nasr wrote:

Thanks so much, that did the trick. Much appreciated.

“F. Carl Sherbino” wrote:

Hi,

Your output is being buffered since you aren’t sending newlines.
So… put a call to flush standard output after you print your line updates
before you sleep…


#include <stdio.h
#include <stdlib.h
#include <unistd.h

int main(int argc, char* argv[])
{
if(argc > 1)
{
int time = atoi(argv[1]);
printf(“time remaining: “);
for(int i = time; i > 0; i–)
{
if(i < 10) printf(”\b\b %i\c”, i);
else printf("\b\b%i\c", i);

Before the sleep, here, add:

fflush(stdout);


sleep(1);
}
printf(“time remaining: 0\n”);
}
return 0;
}
doesn’t print anything until the last line “time remaining: 0”


Chris Nasr
cnasr[at]mechtronix[dot]ca
Mechtronix Systems Inc.


F. Carl Sherbino

Senior Developer
Crisys Limited
8901 Woodbine Ave, Suite 110
Markham, Ontario
Canada
L3R 9Y4

Vox: (905) 474-9111 Ext. 32
Fax: (905) 474-0536


Chris Nasr
cnasr[at]mechtronix[dot]ca
Mechtronix Systems Inc.

Dean Douthat <ddouthat@faac.com> wrote:

Hmm. Does this thread indicate that the Microsoft implementation of printf is
unbuffered by default? Is this POSIX compliant?

I looked into this before – it is allowed. (BTW, printf() is ANSI C,
not Posix. It isn’t an operating system interface.)

The specifications are somewhat vague – most Unixes tend to buffer
stdout as line if to a terminal device, and fully if to a file.
Most DOS compilers tend to not buffer stdout to a terminal
device – not sure about to a file. stderr is always unbuffered.

-David


QNX Training Services
dagibbs@qnx.com