clear the screen!!!
hello does anybody know how i can clear the screen (in console mode) in "c" without having to printf tons of \n's onto the screen?
there must be some kind of function around??? thank you visit: raven.eplay.ch |
Well I've no idea as I dont code in C........
But an option would be to call the system command "clear" That would clear the screen for you. Regards Bomber |
Clearing the screen is terminal specific. The correct way is to use the curses library:
Code:
main() Code:
|
thanks first of all
and to the ncurses slution: if i include the file ncurses.h, the compiler tells me that the functions you told me to enter, arent defined, and so cannot be compiled. do i need to include more libs than ncurses.h? thanx |
What is your exact error?
This is my code: Code:
#include <stdio.h> Code:
# cc -o clearscreen clearscreen.c -lncurses |
well the "undeclared function" issue is over, i just forgot to append -lncurses to the gcc command... :-)
but now i have another one: until now, i can print to the screen fine the command mvaddch() works fine, but how can i tell ncurses to print a value o a variable? until now, i could only print characters referneced by their asci code... thank you |
do you mean...
... you want to keep the string that would be printed with printf to a variable? Then you can use sprintf. It is used exactly as printf only that you have to give an extra argument, the variable that will be used to save the result. 'man sprintf' for more info.
|
Use the printw command. It acts exactly like printf.
Code:
#include <stdio.h> Code:
# ./clearscreen Gary |
now it works fine :-)
thank you. |
Hi Raven,
I guess you already have the answer of your question about clearing the screen, but here's another way : int main (void) { printf ("SCREEN FULL"); system ("clear"); printf ("SCREEN EMPTY"); } CU |
my favorite way since system() is OS dependent and can not be run outside of the Linux/Unix environment is
main() { for(i=0; i<=25; ++i) printf("\n"); } its pretty efficient and it you want you can stick it in its own function and call it whenever you need to clear the screen.. such as.. void cls(void); #include <stdio.h> main() { printf("blah blah\n" "blah blah\n" "blah blah\n" "blah blah\n" "blah blah\n"); cls(); printf("blah blah\n" "blah blah\n" "blah blah\n" "blah blah\n" "blah blah\n"); return(0); } void cls() { for(i=0; i<=25; ++i) printf("\n"); return(0) } Winter |
Your for loop idea will not work in all cases. What if you have a terminal window that is 50 lines long? Do you pick a really high number and hope that it will never go over. Also, notice that if you run 'clear' it will put the cursor at the top of the terminal, not the bottom as will the \n example put you.
|
I think crabboy is right.
clear screen by printing "\n" is not the best way,although it can work in someone's viewpoint. but, different people has different favour. It is your right! There is a chinese idiom: Âܲ·Çà²Ë£¬¸÷ÓÐËù°®£¡ do you understand? ~_~ |
crabboy's solution of
Code:
printf("%c%c%c%c%c%c",27,'[','H',27,'[','J' );} |
Quote:
|
All times are GMT -5. The time now is 05:00 PM. |