Ncurses和Linux管道 [英] Ncurses and Linux pipeline
问题描述
我想用ncurses编写一个简单的程序来显示一些数据.然后,我希望程序以某种方式写入stdout,这样我便可以在命令行上使用管道(|)来管道一些数据了.
I'd like to write a simple program using ncurses for displaying some data. I would then like for the program to write to stdout in such a way that I can then use a pipe (|) on the command line to pipe some data out.
我当前的尝试无效.我可以使用'>'在文件中看到"GOT HERE",但是还有很多其他的东西.该程序也会立即退出.
My current attempt doesn't work. I can see the "GOT HERE"'s in a file using '>', but there's a whole bunch of other stuff. The program also exits immediately.
#include <stdio.h>
#include <ncurses.h>
int main(int _argc, char ** _argv)
{
initscr(); /* Start curses mode */
printw("Hello World !!!"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
printf("GOT HERE");
endwin(); /* End curses mode */
printf("GOT HERE");
return 0;
}
这是使用>
^[[?1049h^[[1;29r^[(B^[[m^[[4l^[[?7h^[[H^[[2JHello World !!!^MGOT HERE^[[29;1H^[[?1049l^M^[[?1l^[>GOT HERE
是否可以通过管道使用stdout
并同时使用ncurses?
Is it possible to use stdout
through a pipeline and ncurses at the same time?
推荐答案
默认情况下,curses写入标准输出,这是您的管道所在的位置.但是,针对诅咒有两种不同的初始化函数: initscr
和 newterm
.后者使您可以执行要求的操作,例如:
By default, curses writes to the standard output, which is where your pipe goes. But there are two different initialization functions for curses: initscr
and newterm
. The latter lets you do what was asked, like this:
#include <stdio.h>
#include <ncurses.h>
int main(int _argc, char ** _argv)
{
newterm(NULL, stderr, stdin); /* Start curses mode */
printw("Hello World !!!"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
printf("GOT HERE");
endwin(); /* End curses mode */
printf("GOT HERE");
return 0;
}
进一步阅读: initscr
.
这篇关于Ncurses和Linux管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!