Ncurses的和linux管道(C) [英] Ncurses and linux pipeline (c)

查看:86
本文介绍了Ncurses的和linux管道(C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写使用ncurses的显示一些数据的简单程序。然后,我会像程序写入到std ::出这样一种方式,然后我就可以使用管道(|)。在命令行上管一些数据出来

I'd like to write a simple program using ncurses for displaying some data. I would then like for the program to write to std::out in such a way that I can then use a pipe (|) on the command line to pipe some data out.

我目前的尝试不起作用。我可以看到使用文件S'>'来到这里,但还有其他的东西一大堆。该方案还立即退出。

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;
}

这是一个使用最终输出>

This is the final output using >

^[[?1049h^[[1;29r^[(B^[[m^[[4l^[[?7h^[[H^[[2JHello World !!!^MGOT HERE^[[29;1H^[[?1049l^M^[[?1l^[>GOT HERE

是否有可能在同一时间使用标准输出通过管道和ncurses的?

Is it possible to use stdout through a pipeline and ncurses at the same time?

在此先感谢

推荐答案

实质上,它无法做到的。基于诅咒的程序使用标准输出的屏幕输出。尝试做别的(如管道标准输出到一些其他的程序)是某种麻烦的食谱。有一个像样的机会,你会通过标准输出诅咒生成的所有控制字符结束;有时,该程序将检测标准输出不是终端和表现不同。但是,如果你需要使用诅咒,你放弃在标准输出使用管道。

Substantially, it cannot be done. A curses-based program uses stdout for the screen output. Trying to do anything else (such as piping standard output to some other program) is a recipe for trouble of some sort. There's a decent chance you'll end up with all the control characters generated by curses on the standard output; sometimes, the program will detect that standard output is not a terminal and behave differently. But, if you need to uses curses, you forego the use of pipe on standard output.

您可能能够得到看中,并安排一个文件描述符,说3,提供给您的程序管道的命令。

You might be able to get fancy and arrange for a file descriptor, say 3, to be available to your program for piping to the command.

curses-program  3>&1 1>&2 | tee your-output-file

在'|'安排的文件描述符1诅咒节目去管。然后,I / O重定向安排文件描述符3去哪里文件描述符1是怎么回事(即管道),然后标准输出去同一个地方标准差(即,终端)。

The '|' arranges for file descriptor 1 of curses-program to go to the pipe. Then the I/O redirections arrange for file descriptor 3 to go to where file descriptor 1 is going (namely the pipe), and then for standard output to go to the same place as standard error (namely, the terminal).

因此​​,你的诅咒方案将标准输出和标准误差去到终端,和文件描述符3可用于写入到管道。那是足够接近你需要什么?在 fdopen()功能将允许你打开文件的描述符文件流。

Thus, your curses program will have standard output and standard error going to the terminal, and file descriptor 3 can be used to write to the pipe. Is that sufficiently close to what you needed? The fdopen() function will allow you to open a file stream for the file descriptor.

这篇关于Ncurses的和linux管道(C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆