LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2002, 08:16 PM   #1
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Rep: Reputation: 31
Smile 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
 
Old 01-15-2002, 10:22 PM   #2
Bomber
LQ Newbie
 
Registered: Jan 2002
Location: Melbourne, Australia
Distribution: Debian
Posts: 13

Rep: Reputation: 0
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
 
Old 01-15-2002, 10:57 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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' );}
 
Old 01-16-2002, 06:00 PM   #4
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Original Poster
Rep: Reputation: 31
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
 
Old 01-16-2002, 10:40 PM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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
 
Old 01-17-2002, 12:12 PM   #6
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Original Poster
Rep: Reputation: 31
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
 
Old 01-17-2002, 03:23 PM   #7
parapente
Member
 
Registered: Sep 2001
Location: Greece
Distribution: Gentoo
Posts: 39

Rep: Reputation: 15
Lightbulb 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.
 
Old 01-17-2002, 11:27 PM   #8
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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
 
Old 01-19-2002, 06:06 AM   #9
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Original Poster
Rep: Reputation: 31
now it works fine :-)

thank you.
 
Old 01-19-2002, 09:39 AM   #10
gluon
Member
 
Registered: Jun 2001
Location: Switzerland
Distribution: SuSE 8.[012], Gentoo
Posts: 42

Rep: Reputation: 15
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
 
Old 05-13-2002, 06:30 PM   #11
Winter
LQ Newbie
 
Registered: May 2002
Location: Washington State
Distribution: Red Hat 7.0
Posts: 22

Rep: Reputation: 15
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
 
Old 05-13-2002, 10:03 PM   #12
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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.
 
Old 05-14-2002, 08:26 AM   #13
wwnn1
Member
 
Registered: Apr 2002
Location: PRC
Posts: 32

Rep: Reputation: 15
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?
~_~
 
Old 05-17-2002, 06:14 AM   #14
Manish
Member
 
Registered: Feb 2002
Distribution: Debian / Debian-based
Posts: 58

Rep: Reputation: 15
crabboy's solution of
Code:
printf("%c%c%c%c%c%c",27,'[','H',27,'[','J' );}
should work for all ANSI-compatible terminals.
 
Old 11-21-2003, 06:28 PM   #15
Avatar33
Member
 
Registered: May 2003
Location: South Africa
Distribution: Ubuntu
Posts: 75

Rep: Reputation: 15
Question

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. :-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i replace the clear "clear screen" with the cls command thefedexguy SUSE / openSUSE 2 12-02-2005 05:02 PM
how to clear screen in C ? indian Programming 3 10-01-2004 10:58 PM
chroot not able to clear screen Archite Linux - Security 6 07-13-2004 10:41 AM
Clear the Screen in C++ OrganicX Programming 16 07-23-2003 04:07 PM
How to clear screen? alexzander Programming 1 01-23-2003 08:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration