clear screen in c

Programming, for all ages and all languages.
Post Reply
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

clear screen in c

Post 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.
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

U....S....E............C....U....R....S.....E....S.....
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post 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!).
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post 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.
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

There is no universally portable way of clearing a terminal 'eboyd'..

Follow Alboin's advice... 8)
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Post 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
Only Human
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Depends ..

Post 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
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Post 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.
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
Post Reply