Hinweis: Das Forum wird geschlossen! Neue Registrierungen sind nicht mehr möglich!

 Zurück zu Pro-Linux   Foren-Übersicht   FAQ     Suchen    Mitgliederliste
Clean Screen in ANSI C

Neuen Beitrag schreiben   Auf Beitrag antworten    Pro-Linux Foren-Übersicht -> Programmieren - C
Vorheriges Thema anzeigen :: Nächstes Thema anzeigen  
Autor Nachricht

BeitragVerfasst am: 04. Apr 2002 0:00   Titel: Clean Screen in ANSI C


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



BeitragVerfasst am: 04. Apr 2002 13:33   Titel: Re: Clean Screen in ANSI C

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? http://www.comeaucomputing.com/techtalk/#clearscreen

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>
// ...

For some versions of Microsoft, you might do this:

#include <conio.h>
// ...

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>
// ...

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

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.


BeitragVerfasst am: 04. Apr 2002 17:25   Titel: Re: Clean Screen in ANSI C

probiers mal damit!

#include <stdio.h>

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

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

#define clrscr() system("cls")


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



mit c-referenz

Beiträge vom vorherigen Thema anzeigen:   
     Pro-Linux Foren-Übersicht -> Programmieren - C Alle Zeiten sind GMT + 1 Stunde
Seite 1 von 1

Gehen Sie zu:  

Powered by phpBB © phpBB Group
pro_linux Theme © 2004 by Mandaxy