c - how to make ncurses program working with other linux utils? -
suppose have ncurses program job on curses screen, , print stdout
. call program c.c
, compiled a.out
.
i expect cat $(./a.out)
first fire ncurses, after action, a.out
quits , print c.c
stdout
, read cat
, , print content of file c.c
.
#include <stdio.h> #include <ncurses.h> int main() { initscr(); noecho(); cbreak(); printw("hello world"); refresh(); getch(); endwin(); fprintf(stdout, "c.c"); return 0; }
i expect ./a.out | xargs vim
, ls | ./a.out | xargs less
work.
but when type ./a.out | xargs vim
, hello world
never shows up. command seems not executed in order, vim not open c.c
.
what correct way make ncurses program work other linux utils?
pipes use standard output (stdout) , standard input (stdin).
the simplest way - rather using initscr
, initializes output use standard output, use newterm
, allows choose file descriptors, e.g.,
newterm(null, stderr, stdin);
rather than
initscr();
which (almost) same as
newterm(null, stdout, stdin);
by way, when include <ncurses.h>
(or <curses.h>
), there no need include <stdio.h>
.
if wanted use program in middle of pipe, more complicated: have drain standard input , open actual terminal device. that's question (and has been answered).
further reading:
Comments
Post a Comment