#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14

    Clear screen and output buffering (console)


    Hey fellas, two quick questions about programming a console app with the stl:

    1) Is there any portable (non system dependent way) to clear the screen of the console? I imagine running a system("cls") call would do the trick in windows, but like I said, I'm looking for portability...

    2) Is there a pre-canned class or object that does output buffering? I'm not talking about io streaming. Say I want to output 200 lines of text, I was wondering if there's anything that does basic text wrapping and that can display one screen at a time while responding to page up/down commands. I can't seem to find anything while pouring through my stl references.

    Regards,
    -Mike
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    Hi Mike! No good news for this...

    1) No. The terminals available when C & unix were invented didnīt have this ability. (those 40x1 text terminals must have looked funny - something like in *old* supermarket registrys LOL)

    2) I guess no because this is not your task. Your OS / terminal should supply this feature.
    i.e. a windows console window will have scrollbars if you setup less lines in the preferences than you actually display. Same for xterms, putty, unix console, ...

    Can I ask what you need this for or is it for educational purposes only?

    hth,
    M.
  4. #3
  5. Doggie
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    Seattle, WA
    Posts
    751
    Rep Power
    13
    For problem #1:
    I'm not sure how cross platform compatible it is, but look at the conio.h header file.

    Here's some of it's functions:

    kbhit() // checks if a key's been hit
    getch() // gets next key in buffer (delays if no key)
    _clrscr(); // clear the screen
    _gotoxy(x,y); // zero based coords
    _wherex();
    _wherey();
    _textcolor(col);
    _textbackground(col);

    For problem #2:
    You'll most likely have to do this by hand. But it shouldn't be too hard to do.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    There isn't a cross-platform way to do console i/o. If you can stick to UNIX systems, the ncurses library can do all of this except word wrapping for you (and word wrapping is an easy algorithm).

    Since the concept of console i/o is antithetical to Windows, I don't think that there are equivalent libraries for Windows. I'm not certain about this though, and it's possible that there has been an ncurses port to Windows.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Code:
    #include <stdio.h>
    
    void my_cls(void) {
       register int i;
       for (i=0; i < 150; i++)
           puts("");
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    Thanks guys.

    Word wrapping is a simple algorithm, no problems there. Heck, I've done that one before, I'm pretty sure that's one of the standard excercies most people learn when they begin to play with pointers.

    The buffering was just to display pages of text. I'm writing a program and going to release it under the gnu gpl. As outlined in the license, if the program is 'interactive', it should be able to display the license when the proper command is typed in. I also wanted to display a few pages of help/instructions when querried to do so.

    I'll check out the resources above, thanks for the suggestions. I'm not really that experienced with 'real' console programming. Any console ones's I've written have traditionally been really standard programs where the iostream objects work fine. No large amounts of text.

    Since you asked, Manuel, it's just a small hobby project. I know a few people who are interested in a program that generates statistics for texas hold'em poker. So I started writing one a few days ago and it's actually turning out to be pretty solid. Got most of the poker logic into it, after that all I have left is to brush up on group theory so I can work through permutations (heh...ok, brushing up is a bad term...I have to learn it). I figure something that's related to gambling like that could potentially get me in trouble if the wrong people get ahold of it, so I want a tight license, not one I just pulled from the air...hence the gnu gpl.
  12. #7
  13. Doggie
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    Seattle, WA
    Posts
    751
    Rep Power
    13
    Originally posted by ClayDowling
    Since the concept of console i/o is antithetical to Windows, I don't think that there are equivalent libraries for Windows.
    The conio.h class works in Windows. When you run a console app in Windows, it simply runs it in a dos window.

IMN logo majestic logo threadwatch logo seochat tools logo