Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clear Console in C 1

Status
Not open for further replies.

OOzy

Programmer
Jul 24, 2000
135
SA
Hello

How can I clear the console in C?

OOzy
 
C doesn't provide a mechanism for this, you'll have to rely on implementation-specific extensions. In DOS, it's typically:

#include <conio.h>

/* ... */

clrscr();

The only portable C way to do this is to write a series of newlines to the display:

int height=80;
int i;

for (i=0;i<height;++i) {
putchar('\n');
}

Russ
bobbitts@hotmail.com
 
What robbitt has said bout the alternate way is really a good logic however the height need not to be 80 rather u can just define it to be 24 as the console has only 25 rows.

SwapSawe.
 
Not every console. I just picked 80 as an arbitrary value. Of course, determining the console height is not anything you can do portably, so you may as well go with a function like clrscr() or write so many newlines that it will be sure to clear *any* console.

Russ
bobbitts@hotmail.com
 
Agreed with rbobbitt and swapsawe and there is also an alternative way

just include this line along with the #include statements

#define &quot;\x1B[2J&quot; CLEAR

and call CLEAR; from inside the program It will clear the screen.
 
I think you mean:

#define CLEAR &quot;\x1B[2J&quot;

I'm curious, in what compiler/environment does this work?

Yes rbobbitt that's right .it works in turbo C++ compiler.
I have not tested it with any other compiler.

Manas


 
Regarding aphrodita's comment, the call to system() will work, with the proviso that you need to provide an argument that is the command-line command to clear the screen in your particular OS. DOS/Windows uses &quot;cls&quot;, but many Unices use &quot;clear&quot;.

If you want portability, you might find it easiest to use the quick-n-dirty method of printing some arbitrary number of newlines.

Share and Enjoy!
 
On UNIX consoles or xterms, printing the following string of chars does the trick. But this code doesnt work on Windows.


Regards,

abp :)
#include <stdio.h>

main()
{

char str[] =
{0x1b, 0x5b, 0x48, 0x1b, 0x5b, 0x4a, '\0'};

printf(&quot;%s&quot;, str);

}

 
For Win32 try this. I am not sure how it ports
to Win 16-bit platform.

Best Regards,

abp :cool:

#include <windows.h>
#include <conio.h>
#define SPACE ' '

int clear_console();

main()
{
clear_console();
return;
}

int clear_console()
{

// Get the handle to Standard Output (STDOUT)

HANDLE hndlStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csBi;

// Fill console screen buffer
GetConsoleScreenBufferInfo(hndlStdOut, &csBi);
DWORD written;
DWORD SZ;

SZ = csBi.dwSize.X * csBi.dwCursorPosition.Y;
// csBi.dwCursorPosition.X + 1;
COORD curhome = {0, 0};

FillConsoleOutputCharacter(hndlStdOut, SPACE, SZ, curhome, &written);

csBi.srWindow.Bottom -= csBi.srWindow.Top;
csBi.srWindow.Top = 0;

SetConsoleWindowInfo(hndlStdOut, TRUE, &csBi.srWindow);
SetConsoleCursorPosition(hndlStdOut, curhome);

return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top