LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   clear the screen!!! (https://www.linuxquestions.org/questions/programming-9/clear-the-screen-11857/)

raven 01-15-2002 08:16 PM

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

Bomber 01-15-2002 10:22 PM

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

crabboy 01-15-2002 10:57 PM

Clearing the screen is terminal specific. The correct way is to use the curses library:
Code:

main()

  initscr(); 
  clear(); 
  refresh(); 
  endwin();
}

This is specific for a linux terminal. I plucked to codes from termcap
Code:

 
printf("%c%c%c%c%c%c",27,'[','H',27,'[','J' );}


raven 01-16-2002 06:00 PM

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

crabboy 01-16-2002 10:40 PM

What is your exact error?

This is my code:
Code:

#include <stdio.h>
#include <ncurses.h>

main()

  initscr(); 
  clear(); 
  refresh(); 
  endwin();
}

compile with:
Code:

# cc -o clearscreen clearscreen.c -lncurses

raven 01-17-2002 12:12 PM

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

parapente 01-17-2002 03:23 PM

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.

crabboy 01-17-2002 11:27 PM

Use the printw command. It acts exactly like printf.

Code:

#include <stdio.h>
#include <ncurses.h>

main()

  initscr(); 
  clear(); 
  printw("I get paid $%08d dollars per hour"
              " at my current job\n", 2 ); 
  refresh(); 
  endwin();
}

Code:

# ./clearscreen
I get paid $00000002 dollars per hour at my current job.

Hope this helps

Gary

raven 01-19-2002 06:06 AM

now it works fine :-)

thank you.

gluon 01-19-2002 09:39 AM

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

Winter 05-13-2002 06:30 PM

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

crabboy 05-13-2002 10:03 PM

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.

wwnn1 05-14-2002 08:26 AM

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?
~_~

Manish 05-17-2002 06:14 AM

crabboy's solution of
Code:

printf("%c%c%c%c%c%c",27,'[','H',27,'[','J' );}
should work for all ANSI-compatible terminals.

Avatar33 11-21-2003 06:28 PM

Quote:

printf("%c%c%c%c%c%c",27,'[','H',27,'[','J' );}
mmmmm Now that I know that it works. Could somebody please explain to me why it works. :-)


All times are GMT -5. The time now is 05:00 PM.