Clearing single line?

This is a discussion on Clearing single line? within the C Programming forums, part of the General Programming Boards category; Is there a way for clearing just a single line? I know clrscr but it clears the whole screen. I'm ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    20

    Clearing single line?

    Is there a way for clearing just a single line? I know clrscr but it clears the whole screen. I'm making a function that prints out ascii art, and I can't get it to start at line 24 printing the first line, while printing the rest all starting at line 24 and going up, so I made it print one line at a time, but I don't know which function lets me clear only one line, so that the first line stays in position while the second line prints out.

    Code:
    void intro()
    {
      int x = 0;
      int y = 24;
      while( y > 0)
      {
      gotoxy(x, y);
      printf("                                                         *  ,   _6gg \\\\ mE0*,\n");
      delay(100);
      if (y != 1)
        clrscr();
      gotoxy(x, y--);
    
      //this is the second line
      printf("                                                            _  -CRMZa- _p#d#K&s\n");  
      }
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    I can't get it to start at line 24 printing the first line, while printing the rest all starting at line 24 and going up, so I made it print one line at a time
    Am I correct in thinking that you want to print from the bottom up? That would only make sense if you focused on getting the bottom part of the drawing right first. If you really want to do it that way, wouldn't it be easier to invert a character matrix and print that the normal way instead?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    If you're wanting that level of control over a console screen, perhaps it's time to consider
    ncurses - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Quote Originally Posted by whiteflags View Post
    Am I correct in thinking that you want to print from the bottom up? That would only make sense if you focused on getting the bottom part of the drawing right first. If you really want to do it that way, wouldn't it be easier to invert a character matrix and print that the normal way instead?
    What I meant to say was print all the lines from line 24 all the way up, while clearing each line. The first line will start from line 24 and end up in line 0, the second from line 24 and going to line 1, and so on.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by louieansonng View Post
    What I meant to say was print all the lines from line 24 all the way up, while clearing each line. The first line will start from line 24 and end up in line 0, the second from line 24 and going to line 1, and so on.
    You almost certainly need a library for that, as Salem suggests, unless there is some simpler way in windows (if you haven't found one by googling, there probably isn't).

    Have a look at ncurses, that's the kind of thing used with console apps where you have color and menus & stuff. So it's more work that a single command but you might find it interesting; you have total control over the text console window and can respond to key presses, etc. It's easier than GUI programming but similar in nature.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Quote Originally Posted by MK27 View Post
    You almost certainly need a library for that, as Salem suggests, unless there is some simpler way in windows (if you haven't found one by googling, there probably isn't).

    Have a look at ncurses, that's the kind of thing used with console apps where you have color and menus & stuff. So it's more work that a single command but you might find it interesting; you have total control over the text console window and can respond to key presses, etc. It's easier than GUI programming but similar in nature.
    Will I be able to compile it like a normal c file? I'm using Dev-C++ and our project specification says that we can do anything we want as long as we can compile it without problems in Dev-C++ compilers.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by louieansonng View Post
    Will I be able to compile it like a normal c file? I'm using Dev-C++ and our project specification says that we can do anything we want as long as we can compile it without problems in Dev-C++ compilers.
    I don't know. You'll have to download and install the package and follow the directions there; unless there is something weird about Dev-C++ there is no reason it shouldn't work.

    Make sure you link properly to ncurses when you compile.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Quote Originally Posted by MK27 View Post
    I don't know. You'll have to download and install the package and follow the directions there; unless there is something weird about Dev-C++ there is no reason it shouldn't work.

    Make sure you link properly to ncurses when you compile.
    I just downloaded the package and got really confused about all the files there. There wasn't a readme file on which files to put on which folder. I'll have to ask my professor on the details.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If your program requires ncurses, then your teacher will need ncurses in order to compile it.

    You don't need a special library to do some line clearing of the screen, anyway. If you move the cursor to the first char of the row you want to erase, and print out 80 char's of ' ', then the line will be cleared.

    To move the cursor you can:

    1) include <conio.h> and use gotoxy(x,y) (Which Dev C may not support since it's non Standard).

    2) In Windows, you use SetConsoleCursorPosition(), This is an example showing color and moving the cursor:

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <wincon.h>
    
    
    void Gotoxy(int, int);
    
    int main(void)  {
       int x, y;
       
       char *starlight = "Oh Be a Fine Girl Kiss Me";
       char *ilike = "I like blue stars";
       for(x = 0, y = 1; x < 41;)  {
          Gotoxy(x,y);
    	  printf("%s", ilike);  
    	  x += strlen(ilike)+1;
    	  
       }
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | BACKGROUND_RED | BACKGROUND_BLUE |BACKGROUND_GREEN);	
       printf("\n%s\n ", ilike); 
       for(y = 4, x = 10; y < 18; y+=5)  {
          Gotoxy(x, y);
          printf("%s", starlight); 
          SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE |  FOREGROUND_RED | BACKGROUND_RED);
       }
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED|BACKGROUND_BLUE);	
       printf("\n\n\n%s", starlight);
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE|BACKGROUND_BLUE);	
       printf("\n\n");   
       return 0;
    }
    
    
    void Gotoxy(int x, int y) {
       COORD coord;
       coord.X = x;
       coord.Y = y;
       SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    /*
       RED + GREEN + BLUE = White	
       RED + GREEN = Yellow
       RED + BLUE = Magenta
    
       GREEN + BLUE = sickly Green
    */
    Printing from the bottom up is generally to be avoided, as WhiteFlag noted, above. If you want to start your drawing at the bottom of the page, you can always draw it starting at the bottom of the page, (use a series of newlines to get there), and printing it normally, let it continue to cause the screen to scroll up, as you continue printing your lines, in top to bottom order.
    Last edited by Adak; 07-26-2009 at 09:52 AM.

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Isn't there a way of doing this by using the carriage return character (\r on Windows)?

    I'm pretty sure the escape character stood for "replace line" or something among those lines.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Adak View Post
    You don't need a special library to do some line clearing of the screen, anyway. If you move the cursor to the first char of the row you want to erase, and print out 80 char's of ' ', then the line will be cleared.
    That would assume that the console window is always 80 characters wide.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Quote Originally Posted by cpjust View Post
    That would assume that the console window is always 80 characters wide.
    Yes, our console window is 80 x 25. Maybe I'll follow adak's way of creating lines...I don't want to waste my time doing something that might not work in our teacher's computer. Thanks for all the suggestions.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by louieansonng View Post
    Yes, our console window is 80 x 25. Maybe I'll follow adak's way of creating lines...I don't want to waste my time doing something that might not work in our teacher's computer. Thanks for all the suggestions.
    You can also use the backspace:
    Code:
    int main() {
    	char ray[4] = {8,8,8,0};
    	printf("there%s",ray);
    	return 0;
    }
    output:
    th
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MK27 View Post
    You can also use the backspace:
    Code:
    int main() {
    	char ray[4] = {8,8,8,0};
    	printf("there%s",ray);
    	return 0;
    }
    output:
    th
    That code also assumes you're on an ASCII machine.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  15. #15
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    What platform are you working on? If it's a Unix/Linux variant, enable vt100 terminal mode emulation and printf() the escape sequence which clears a single line on the terminal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  2. ASM beginner gets 24 compiler errors on hello world...
    By Desolation in forum Tech Board
    Replies: 12
    Last Post: 06-16-2007, 10:21 PM
  3. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21