Clearing the screen natively?
I just have a question how one would clear the output from a console application in nativly. I use the system call function in the windows.h header file but i would like to know how to just 'clear it' so i can port this to linux. Any help would be greatly appreciated.
There is no portable way to clear the screen
unless you do something like
if you know what i mean...
unless you do something like
#ifdef WINDOWS ...#else ...
if you know what i mean...
There used to be a function in conio.h (clrscr?) but I don't think Microsoft supports it anymore.
A quick search turned up these:
"pritnf("\033[2J");"
Maybe this too?
"pritnf("\033[2J");"
// clrscr.cpp // // adapted from // Vincent Fatica // vefatica@syr.edu // Syracuse University Mathematics // http://barnyard.syr.edu/~vefatica/ // // by Shannon Bauman // August 9, 1998 // // clear entire console screen buffer#include <windows.h>#include <conio.h>// don't know if it's really necessary//I can run it without it, though...int clrscr(){HANDLE hndl = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_SCREEN_BUFFER_INFO csbi;GetConsoleScreenBufferInfo(hndl, &csbi);DWORD written;DWORD N = csbi.dwSize.X * csbi.dwCursorPosition.Y +csbi.dwCursorPosition.X + 1;COORD curhome = {0,0};FillConsoleOutputCharacter(hndl, ' ', N, curhome, &written);csbi.srWindow.Bottom -= csbi.srWindow.Top;csbi.srWindow.Top = 0;SetConsoleWindowInfo(hndl, TRUE, &csbi.srWindow);SetConsoleCursorPosition(hndl, curhome);return 0;}
Maybe this too?
void cls(){_asm {mov AH,15int 10hmov AH,0int 10h}}
Quote:Original post by Xiachunyi
A quick search turned up these:
"printf("\033[2J");"
*** Source Snippet Removed ***
The print("\033[2J"]); requires ANSI.SYS or similar compatible support to work, I believe. (Loaded in CONFIG.SYS.)
The source snippet I think is from MSDN and works without ANSI.SYS because it literally writes spaces over the entire screen.
Another ANSI.SYS option from http://home.fhtw-berlin.de/~junghans/cref/EXAMPLES/clrscr.c
/************************************************************************* * * Purpose: Clear the screen with VT100 escape codes. This can be done * with conio.h on PCs - non standard code. Or curses.h, bit of * a fag... * Author: M.J. Leslie * Date: 22-Jun-94 * ************************************************************************/void clrscr(void);main(){ clrscr();}void clrscr(void){ char a[80]; printf("\033[2J"); /* Clear the entire screen. */ printf("\033[0;0f"); /* Move cursor to the top left hand corner */}
Many libraries include their own version of clrscr() defined in conio.h. You might check to see if whatever environment you're using has it predefined. (Most folks gravitate towards a cls() command.)
I'm pretty sure GCC has a clrscr() function, as well as Turbo C++ v4.5 and Borland C++ 4.5/5.0/5.5. I've never tried it under any of the Visual Studio products - I tend to use them to create graphical Win32 applications.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement