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
#include
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;
}
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()
.