3

I am using C language. I want to display a table. I know how to display a normal table using printf. However I have many records (more than several thousand), and I want to keep two things on screen all the time and have the middle portion of record keeps updating...

Suppose here is a table:

================================
No.| Name | Some Data | Comments
================================
001   xyz     234        abc
002   uvw     5862       abc
003   lmn     8993       abc
004
005
006
007
...
...
================================
Some result analysis goes here: 
================================

Now lets assume there are thousand of entries in this table, and in less than a second this table is updating, eventually what will happen? I will loose the header and footer of table like: No, Name , Some data and comments, and in footer result analysis..

What I want is to keep updating the entries but have the header and footer of the table remain. How can I implement this! Any idea?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Karl Marks
  • 33
  • 2
  • The curses solutions below seem best, but if they seem daunting, I wonder if it is acceptable to repeat the header rows in the output? That would probably be simpler - you could print the header, print 18 rows, print the header, print the next 18 rows, etc, until the end where you print the footer. Not pretty but simpler. – Rob I May 31 '12 at 17:21

3 Answers3

2

I would look at a TUI library like curses.

Curses is designed to facilitate GUI-like functionality on a text-only device, such as a PC running in console mode, a hardware ANSI terminal, a Telnet or SSH client, or similar.

In Curses, you can create regions on the screen hold the header, footer, and data, and handle some keyboard keys to implement scrolling through the rows. Presumably other TUI libraries have similar functionality.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
1

Assuming you are on a Unix environment with a typical terminal, you can use GotoXY().

With the function, you can move the cursor up to the top row (below the headers), print a bunch of rows, then return back to the top row again to print more.

You'll have to keep track of how many rows you've printed (and how many rows will fit), but that should be some relatively trivial counting.

The code uses Terminal Escape Sequences to control cursor movement.

abelenky
  • 63,815
  • 23
  • 109
  • 159
1

Are you using a Unix/Linux system? If yes, probably you have ncurses library (man 3). It is a library to manipulate character interface.
You are able to create 'windows' (screen sections) and update only this parts (to remain the header and footer).

  • FWIW there are versions of curses that run on Windows/DOS - see http://stackoverflow.com/questions/138153/is-ncurses-available-for-windows – dsolimano May 31 '12 at 16:49
  • Thankyou Andre, I was looking for this type of solution i had a look.. its just awesome.. ncurses.. Thanks to all.. :) – Karl Marks May 31 '12 at 17:32
  • You're welcome. Thanks @dsolimano, i didn´t know that (n)curses can be used into DOS/Windows systens (PDCurses looks great!). – André A. G. Scotá May 31 '12 at 18:02