Printf () / puts () after using ncurses

I use the library ncursesin C project and ran into a problem using printf()/ puts()after cursing and initializing . Here is a simplified illustration:

initscr();
endwin();

puts("first");
usleep(1e6);
puts("second");

Both firstand secondwill appear on the screen just after the executable file will (a little more than one second), instead of having to first print firstand then, after a second second. ncursesit seems to buffer stdoutsomehow and only flush it when exiting. fflush(stdout)seems to solve the problem:

initscr();
endwin();

puts("First.");
fflush(stdout);
usleep(1e6);
puts("Second");

stdout , ( ). puts() usleep() , fflush(stdout) , , , , , .

+4
1

ncurses setvbuf, . NCURSES_NO_SETBUF, , , setvbuf, man 3 setvbuf :

setvbuf() , (, ), "". -.

stdout stderr :

#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;
}
+4

Source: https://habr.com/ru/post/1546082/


All Articles