Page 1 of 1

clear screen in c

Posted: Sat Sep 22, 2007 10:17 am
by eboyd
I've been advised to avoid the "system calls" such as

Code: Select all

int main()
{
    system("pause");
    system("cls");
    return 0;
}
Now for Cygwin, i'm able to make the "programs" pause & clear which do the same thing as the above. Obviously pause isn't exactly like "system("pause")" but it gets the job done well enough for my purposes.

pause:

Code: Select all

#include <iostream>
int main(int argc, char *argv[])
{
    std::cout << "Press [Enter] to continue...";
    std::cin.ignore( 256, '\n' );
    return 0;
}
and clear as:

Code: Select all

#include <stdio.h>
int main(int argc, char *argv[])
{
    printf("\033[2J");
    printf("\033[1;1H");
    return EXIT_SUCCESS;
}
Is there a way to write "clear()" in a void function, NOT in its own executable like so:

Code: Select all

void clear()
{
    printf( "\033[2J" );
    printf("\033[1;1H");
}
So I can include pause and clear in my own executables?

Because right now, clear in a void function just prints something like "<-[2J" instead of actually clearing the screen.

Posted: Sat Sep 22, 2007 11:34 am
by Alboin
U....S....E............C....U....R....S.....E....S.....

Posted: Sat Sep 22, 2007 12:03 pm
by eboyd
U....S....E............C....U....R....S.....E....S.....
Yea but I'm trying to avoid any more dependencies. Plus, I'd like something that will work with not only gcc, but msvs and others. I'm not sure what else curses has been ported too, so I'm just assuming there is no equivalent for msvs (and i'm certainly not going put windows.h anywhere in my code!).

Posted: Sat Sep 22, 2007 12:14 pm
by Alboin
eboyd wrote:
U....S....E............C....U....R....S.....E....S.....
Yea but I'm trying to avoid any more dependencies. Plus, I'd like something that will work with not only gcc, but msvs and others. I'm not sure what else curses has been ported too, so I'm just assuming there is no equivalent for msvs (and i'm certainly not going put windows.h anywhere in my code!).
Pdcurses works on everything.

Posted: Sat Sep 22, 2007 12:16 pm
by Brynet-Inc
There is no universally portable way of clearing a terminal 'eboyd'..

Follow Alboin's advice... 8)

Posted: Thu Sep 27, 2007 3:11 am
by Neo
If you can determine no. of lines

Code: Select all

for(int i=0; i< numOfLines: ++i)
  printf("\n");
should be the best :D

Depends ..

Posted: Sun Sep 30, 2007 12:33 am
by DeletedAccount
if your's using the archaic turboc then
clrscr() in conio.h does the job

in unix use ncurses ....

The reason for not using system() library functions is mainly security
reasons ... eg If a process has a greater privilagge level and
it has something like .. gets(input);
system(input);
you can easily get the root shell by typeing yourcommand;su

So another solution would be ...
1) use CreateProcess in Windows to run cls
2) use fork and exec in unix to run clear

Posted: Sun Sep 30, 2007 3:58 am
by B.E
Neo wrote:If you can determine no. of lines

Code: Select all

for(int i=0; i< numOfLines: ++i)
  printf("\n");
should be the best :D
In terminals that have scroll history, this can produce undesired results. also the cursor is not in the correct position.