tags:

views:

328

answers:

4

I have a little routine that's run under Linux and Windows written in C and displays output on the console. I'm not linking in any form of curses or anything like that.

Currently I clear the screen using

#ifdef __WIN32
  system( "cls" );
#else
  system( "clear" );
#endif

Then I have a bunch of printf statements to update the status. What I'd like just reset the screenpointer to 0,0 so I can then just overlay my printfs. I'd rather avoid compiling in any more extensions especially since I'm coding for 2 different OS'.

A: 

Looks like I may have found a windows specific way of doing it SetConsoleCursorPosition

Ansi escape sequence \033[0;0H for Linux - just printf that to the console.

Paul Hargreaves
The ansi escape isn't guaranteed for Linux, in the small chance a different terminal type is used. You may want to verify the feature within Termcap or Terminfo.
Raymond Martineau
A: 

For Unix-like platforms, the usual way to do this is using the curses library.

Greg Hewgill
A: 

Yes, for unix platforms, curses (or ncurses, these days) is the way to go. And there are versions that work under windows, so you could do it the same way on both systems.

wnoise
A: 

For windows - You can use ANSI escape characters.

http://www.lexipixel.com/news/star_dot_star/using_ansi_escape_sequences.htm

http://www.robvanderwoude.com/ansi.html

printf "\x[0;0H"

It used to be that Ansi.sys needed to be loaded before you could do this, but it's worth a shot.

Instructions for adding ANSI support http://www.windowsnetworking.com/kbase/WindowsTips/WindowsXP/UserTips/CommandPrompt/CommandInterpreterAnsiSupport.html Note: that Ansi.sys only works under command.com. You can't use it with cmd.exe

seanyboy