Thread: Using the system("clear") command in Vista

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    Using the system("clear") command in Vista

    i am a C newbie attempting to clear the screen in a program. When I wrote a quick test program,
    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    
    {
    
    system("clear");
    
    }
    it compiled perfectly, but when I ran it gave the error message "sh: clear: command not found". I also tried the same but replacing "clear" with "cls", but received a similar error message. Can anyone help with this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What compiler are you using? "sh: " seems like you are using Cygwin, which simulates a unix environment.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    yes i'm using cygwin

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You really ought to look around for help with the problem you are having with cygwin, maybe someone in the cygwin community has had this problem before you. The web would know. Perhaps do it in a portable way in the meantime?
    Code:
    void doclear (void)
    {
      int i;
      for (i = 0; i < 1200; i++) {
        putchar('\n');
      }
      fflush(stdout);
    }

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    thanks citizen, 'til i get it sorted online that works fine. looks a bit scrappy when it runs but it'll do for now

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why 1200? The default number of lines of history in Vista is 500, I'm pretty sure. (Though I set mine to 1000.)

    You could use the ASCII escape sequence to clear the screen. I think this is it. http://www.cygwin.com/ml/cygwin/1999-08/msg00285.html
    It should work on Vista, I would think.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> Why 1200?

    I didn't think about it that hard, I just wanted to reach the bottom of most monitors, and 1200 is the tallest height in pixels that came to mind.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    . . . but we're talking lines here. A line is usually at least 8 pixels high.

    If you want to handle standard terminals, print 25 newlines. That should work on most DOS and Linux terminals. Unless the user has made their terminal larger, of course, but then it's their problem.

    Personally, I dislike programs that clear the screen. The history is there for a reason -- it serves to show what you've typed in the past! If you have sensitive information, like a password, try to figure out an alternative way to display it, such as not echoing the characters -- which also happens to be non-standard.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    By the way op, I did find something else. Looks like you need to install a library called ncurses.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by dwks View Post
    If you want to handle standard terminals, print 25 newlines.
    Or 50 for full screen mode.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    But that depends on the user's screen resolution, and we've come in a circle.

    Why not just install ncurses? . . . .

    BTW, you might want to use something like this for semi-better portability:
    Code:
        system(
    #ifdef WIN32
            "cls"
    #else
            "clear"
    #endif
            );
    At least that way the program will still work if you compile it with Dev-C++ . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It doesn't depend on the resolution, as the width is still 80 characters. It does however depend on the font.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think you're confused. The code is printing a number of newlines. The width of the screen does not matter a bit -- a newline scrolls down a line, no matter how wide that line is or what was previously printed on the line.

    What does matter is how many lines are visible on the screen at once -- that is, how tall the console is vertically. Usually this is 25 for DOS or 24 for Linux, but it varies, of course.

    Making a console fullscreen, in XP, at least, increases the height of the console window to the height of the screen. The number of lines the console is at this point does indeed depend on your screen resolution.

    Therefore, the number of newlines that you have to print to clear the screen varies depending on the screen resolution, if you assume you users will be messing with the size of the console.

    Personally, I'd go with 25, or better yet, install clear, which does a much better job. (It resets the cursor position to the upper-left corner of the screen as well as clearing it.)

    [edit] One thing I didn't think of -- were you talking about ALT-ENTER fullscreen mode? In that case, you might be right -- although on my old Windows 98 computer, a fullscreen terminal is still in the same mode (80x25, I think) by default. Switching to fullscreen shouldn't affect the number of lines displayed.

    Which might be what you were referring to.

    Except, of course, that the font has no affect on this either. Unless larger numbers of lines cause the font to look smaller. (Do you have a CRT?)

    I think I'm overanalyzing something here. [/edit]
    Last edited by dwks; 08-13-2008 at 08:12 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yeah I was referring to Alt+Enter. It's 80 by 50 on XP at least. Vista console does not support it, unfortunately. Cygwin does though. I imagine it would have to use the same dimensions to look right, although it may use a taller font.

    I just remember this from when I was doing a full screen ascii program. It was 80x50-1 characters to refresh the screen.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yeah, I was trying to run a bunch of old full-screen DOS programs on Vista the other day. It didn't work. I guess Microsoft decided that they opened up too many security holes.

    It sounds like you're talking about clearing the screen with spaces. Note that you don't have to: ordinary newlines work just fine. And then you only have to worry about the screen's height, not its width as well.

    Another note: I don't think printing newlines or spaces properly clears the colour attributes of a Windows console. Another thing that clear and cls do that printing chars does not.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. corruption, decay, Vista Gateway laptop
    By CodeMonkey in forum Tech Board
    Replies: 5
    Last Post: 06-13-2009, 01:50 PM
  2. Newbee Q: reg. MFC dlg button & Vista UAC shield icon
    By colbyringeisen in forum Windows Programming
    Replies: 3
    Last Post: 12-29-2008, 05:16 PM
  3. Sound worked in XP; not in Vista
    By lostpuppy in forum Windows Programming
    Replies: 6
    Last Post: 08-12-2008, 12:25 AM
  4. get "application data" directory in xp, vista
    By sgh in forum Windows Programming
    Replies: 8
    Last Post: 05-12-2008, 02:48 AM
  5. First Vista virus
    By mrafcho001 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-11-2005, 10:39 PM