PDA

View Full Version : clear screen in C


boris
07-23-2004, 02:17 PM
how do you clear the console screen in C?
itried system("cls") but it didn't work

ronburk
07-23-2004, 06:52 PM
How about some Console API functions? (http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q99/2/61.asp&NoWebContent=1)

sad69
07-23-2004, 06:57 PM
I think this is it on Linux:
printf(" \033[2J");

Read here for information on why escape sequences are bad (as the above solution is an escape sequence):
http://forums.devshed.com/archive/t-141701

The other solution is to write a function called like this:

void clear_screen() {
int i;

for(i = 0; i < 50; i++)
printf("\n");
}


Hope that helps,
Sadiq.

boris
07-24-2004, 12:55 AM
i dont want to cheat by printing repeated new lines, i want to actually clear the console.
and i pretty much want this to work in both windows and UNIX. if i cant have both, then just windows

Jason
07-24-2004, 01:02 AM
#include <conio.h>
#include <stdlib.h>
#include <iostream.h>

int main()
{
cout << "abc" << endl
<< "abc" << endl
<< "abc" << endl
<< "abc" << endl;
getch();

system( "cls" );
return 0;
}


did you have the proper includes?


Jason

boris
07-24-2004, 04:43 AM
i did include stdlib.h cos thats where it is
it works for windows now, i was mucking around with something else before..
but will it work for linux/unix?

btw: isnt cout and cin C++ commands
i only use C atm

Celtboy
07-24-2004, 07:54 PM
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>

#define VIDEO 0x10
#define COLS 80
#define ROWS 25

void cls(void);
void locate(int row, int col);

void main() {
puts ("Random Text");
cls();
}

void cls(void) {
union REGS regs;

regs.h.ah=0x06;
regs.h.al=0x00;
regs.h.bh=0x07;
regs.h.ch=0x00;
regs.h.cl=0x00;
regs.h.dh=ROWS-1;
regs.h.dl=COLS-1;
in86(VIDEO,&regs,&regs);

locate(0,0);
}


void locate(int col, int row) {
union REGS regs;

regs.h.ah=0x02;
regs.h.bh=0x00;
regs.h.dh=row;
regs.h.dl=col;
int86(VIDEO,&regs,&regs);
}

uhm...that might work...lol....*might* :P