Sign in to follow this  
mxrss

Clearing the screen natively?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
A quick search turned up these:

"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,15
int 10h
mov AH,0
int 10h
}
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this