Clean Screen in ANSI C

Post Reply
Message
Author
Daracher

Clean Screen in ANSI C

#1 Post by Daracher »

Hi.

ich moechte ein portables Programm in C schreiben, das unter anderem einen Clean Screen beinhaltet. Koennt ihr mir weiter helfen? Moegliche Loesungen unter den jeweiligen Platformen (windows/*nix) hab ich schon gefunden, jedoch noch keine, die unter allen funktioniert.
Schon mal vielen Dank

Daracher

Descartes

Re: Clean Screen in ANSI C

#2 Post by Descartes »

How do I clear the screen?
How do I change the color of the screen?
How does one erase the current line...And questions like these? <a href="http://www.comeaucomputing.com/techtalk/#clearscreen" target="_blank"><!--auto-->http://www.comeaucomputing.com/techtalk ... <!--auto-->

Neither Standard C nor Standard C++ provide such a capability. It is considered beyond their scope due to the diversity in operating systems, since a screen or terminal or monitor or console might not even exist, etc.

That being so, many compilers, or operating systems, do support an extension in order to be able to clear the screen, or erase the current line. For instance, with Borland, you might do this:


#include <conio.h>
// ...
clrscr();

For some versions of Microsoft, you might do this:

#include <conio.h>
// ...
_clearscreen(_GCLEARSCREEN);

One of these will also often work on some systems:

#include <stdlib.h>
// ...
system("cls"); // For Dos/Windows et al "console app"
system("clear"); // For some UNIX's, LINUX, etc.
system("tput clear"); // For some UNIX's
// ...

effectively running the command in quotes as a process. Tied in with this, you could also create a shell script named cls which internally will do the clear or tput clear, and so on. Note that <stdlib.h> might be <cstdlib> in the case of C++, and so therefore references to system() above would be std::system().
You can also just output a known number of newlines in some cases, if you know the number:

// ...
const int SCREENSIZE = ???;
for (int i = 0; i < SCREENSIZE; i++)
{
// output a newline, perhaps with std::cout << '
';
}
// flush the output, perhaps with std::cout.flush();

You can also just hard-code an escape sequence, though this too may not be portable:

std::cout << "\033[2J" << std::flush;

This of course means too that something like this could be done in VC++ as well:

#include <conio.h>
// ...
_cputs("\033[2J");

There is also the "curses" (or ncurses in some cases) system, which, although not Standard, was popular with UNIX before X-windows became popular:

#include <curses.h>

int main()
{
initscr(); // set up a curses window
clear(); // clear the curses window
refresh(); // commit the physical window to be the logical one

// stuff using the window

endwin();
return 0;
}

and/or various different graphics and windowing systems, etc.
If it's not obvious to you yet, whether you're clearing the screen, erasing the current line, changing the color of something, or whatever, you'll need to concede that no one way is portable, and that to accomplish what you need you'll have to find a platform specific solution in most cases. Check with your compiler or operating system vendor's documentation for more details. And never discard ingenuity when it's necessary. For instance, on some displays you may get away with emitting a form feed, or a special escape sequence. Or, if you have a primitive device you may have to do something like figure out how many newlines/spaces/tabs/whatever, to emit in order to scroll the screen away, though the cursor may not be where you prefer it. And so on.

Last but not least, construct your code sensibly. If you really do need say the ability to clear the screen, don't lace system dependent calls through your code, or even #ifdefs. Instead, write a function to encapsulate the call, so that if you do need to localize it in some way, at least it's just in one place.

badwolf

Re: Clean Screen in ANSI C

#3 Post by badwolf »

probiers mal damit!

#include <stdio.h>

#ifdef __unix__
#define clrscr() printf("\x1B[2J")

#elif __BORLANDC__ && __MSDOS__
#include <conio.h>

#else
#define clrscr() system("cls")

#endif

int main()
{
//So müsste es funktionieren!

clrscr();
}

BadWolf

www.badwolf.de.vu
mit c-referenz

Post Reply