clear screen for text – 3 ways in C

I have been wondering the implementation of top’s (I mean ‘top’ command) displaying for long – like a text ‘GUI’ on the screen with numbers changing all the time. Though still I have not got time to go thru the source of ‘top’, I find some other ways to do that. Now imagine if you could clear screen after the each displaying and output the results right to the same beginning position of the screen, you have got a ‘top’-like displaying indeed.(用curses库实现文字清屏-’top’命令的显示风格)

1.  Shell command ‘clear’

This is the easiest way to do.  With another C lib function ‘system‘, you can implement the displaying like below:

// Clear the terminal output
// Then printf stuff……

However, you will find this is not real ‘top’-like displaying – ‘clear’ indeed always makes the output right from (0, 0) (the top left corner of the screen). But you still can find the previous output by scrolling back the window and all the output is re-displayed again as we are using ‘printf’ – that is why there should be a flash on screen during each displaying. We will rise the bar – is there a way to fix the things unchanged on screen but redisplay the things changed?

2. Lib curses/ncurses

‘curses’ lib was originated by Bill Joy and Ken Arnold. ‘ncurses’ lib is an enhancement version of ‘curses’ by GNU. All we need to do is: 1. #include <curses.h> (We do not include <ncurses.h> as we may need to support both Solaris and Linux). 2. cc -o a.out src.c -lcurses. Now we could code in this way:

initscr(); //start curses mode
for ( i = 0; i < 100; i++)
clear(); //This is clear from curses lib
printw(“davetin”); // curses’ printf stuff
printw(“i = %dn”, i);
refresh(); // display on the screen
endwin(); // quit curses mode

When running this episode of code, you will find – 1. no previous output even scrolling back the window; just one output on the screen; 2. except the changing ‘i’, others are remain static! That is cool as we could write the same thing like ‘top’ displayed. The hidden thing behind is concept of ‘virtual’ window. When we call ‘initscr’, we make a new ‘virtual’ window called ‘stdscr’. All the things we try to display via ‘printw’ will NOT be written to standard output unless ‘refresh’ is called.  More of that, ‘refresh’ would make sure only the changed things be refreshed on the screen. Now what? Color – no problem; Highlight – no problem……

3. cleardevice/clrscr

The 2 functions needs ‘#include <conio.h>‘, which is NOT a standard C header file. However, it is usually included by TurboC and Windows.  ‘cleardevice’ is used to clear graphic screen, which ‘clrscr’ is used to clear text screen.

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Programming and tagged , . Bookmark the permalink.

7 Responses to clear screen for text – 3 ways in C

  1. mercy..after a more thorough search, they also found a kegger.. :}

  2. psycholog says:

    Jon K – you said “Any Canadian would totally rather be living in the tropics pursued by ravenous giant monsters than have to live in Canada, you know?”

  3. psycholog says:

    Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

  4. A pity, but I dispute with this unique piece of writing. I do like your specific web-site however and will likely keep on moving again for news.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.