c - printf()/puts() after use of ncurses -
i'm using ncurses
library in c project, , encountered problem use of printf()
/puts()
after curses initialized and de-initialized. here's simplified illustration:
initscr(); endwin(); puts("first"); usleep(1e6); puts("second");
both first
, second
appear on screen after containing executable exits (after little on 1 second), instead of printing first
first, , then, second later, second
. ncurses
seems buffering stdout
somehow , flushing on exit. fflush(stdout)
seems solve problem:
initscr(); endwin(); puts("first."); fflush(stdout); usleep(1e6); puts("second");
when stdout
manually flushed, output displayed expected (with second gap). if add more puts()
statements afterwards usleep()
s in between, though, i'd need repeat calls fflush(stdout)
after each one, , i'm wondering if there's better solution that, say, permanently resets program pre-curses mode.
ncurses
calls setvbuf
, putting streams full buffered mode. either specify environment variable ncurses_no_setbuf
instruct not change buffering, or restore buffer mode calling setvbuf
again, although man 3 setvbuf
advises against this:
the setvbuf() function may used @ time, may have peculiar side effects (such discarding input or flushing output) if stream ``active''. portable applications should call once on given stream, , before i/o performed.
here how restore stdout
line-buffering , stderr
no buffering again:
#include <stdio.h> #include <unistd.h> #include <ncurses.h> int main() { initscr(); endwin(); setvbuf(stdout, null, _iolbf, 0); setvbuf(stderr, null, _ionbf, 0); puts("first."); usleep(1e6); puts("second"); return 0; }
Comments
Post a Comment