如何连续刷新屏幕并实时更新 [英] How to refresh the screen continuously and update it in real time

查看:146
本文介绍了如何连续刷新屏幕并实时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Linux上编写一个C程序,该程序连续刷新屏幕并实时更新它(例如,类似于终端中的top命令).谁能指出我正确的方向.

I want to write a C program on linux which refreshes the screen continuously and updates it in real time (for example, similar to the top command in the terminal). Can anyone point me in the right direction.

推荐答案

要使其在各种终端类型之间均可移植,您需要使用诸如

To keep it portable across terminal types, you need to use a library such as ncurses. Check out that link, its an exhaustive tutorial.

这是一个基本程序,可以在屏幕的左上角打印出越来越多的数字:

Here is a basic program that prints an ever increasing number at the top left corner of the screen:

#include <stdio.h>
#include <ncurses.h>

int main (void)

{
        /* compile with gcc -lncurses file.c */
        int c = 0;
        /* Init ncurses mode */
        initscr ();
        /* Hide cursor */
        curs_set (0);
        while (c < 1000) {
                /* Print at row 0, col 0 */
                mvprintw (0, 0, "%d", c++);
                refresh ();
                sleep (1);
        }
        /* End ncurses mode */
        endwin();
        return 0;
}

这就是刷新窗口的方式.现在,如果要像top一样显示数据行,则需要将显示的数据保持在有序的数据结构中(取决于数据,它可能像数组或链表一样简单).您必须根据逻辑指示对数据进行排序,并在clear()wclear()之后重新写入窗口(如上例所示).

Thats how you refresh the window. Now, if you want to display rows of data as top does, the data you display needs to be maintained in an ordered data-structure (depending on your data, it maybe something as simple as an array or a linked list). You would have to sort the data based on whatever your logic dictates and re-write to the window (as shown in the example above) after a clear() or wclear().

这篇关于如何连续刷新屏幕并实时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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