5

Is it possible to clear multiple lines in C and keep others for example.

Code:

Displaysenrsordata
  loop 
    printf("This info stays"); <-stay on screen
    printf("This info stays"); <-stay on screen
    printf("This info Refreshes"); <-update redraw
    printf("This info Refreshes"); <-update redraw
    printf("This info Refreshes"); <-update redraw

Essentially I want to have some text to stay at the same place and redraw the updating data without clearing the whole screen.

Benjamin Bannier
  • 55,163
  • 11
  • 60
  • 80
user1031204
  • 701
  • 1
  • 8
  • 30
  • 2
    You need to specify your environment (Unix terminal/Windows console app/...) as this is beyond ISO C. – ninjalj Dec 15 '13 at 13:27

3 Answers3

6

If you are working on linux then use ncurses.

Example:

#include <stdio.h>
#include <ncurses.h>
  int main (void)
  {
    int a = 0;
    initscr ();
    printw("This info stays \n");
    printw("This info stays\n");
    curs_set (0);
    while (a < 100) {
            mvprintw (3, 4, "%d", a++);
            mvprintw (3, 8, "%d", a++);
            mvprintw (3, 12, "%d", a++);
            refresh ();
            sleep (1);
    }
    endwin();
    return 0;
 }
Chris Page
  • 18,263
  • 4
  • 39
  • 47
sujin
  • 2,813
  • 2
  • 21
  • 33
3

You can overwrite the current line be printing out a \r, or the last character on the current line by printing a \b.

alk
  • 69,737
  • 10
  • 105
  • 255
-5

No, you can't clear only a part of the console window.

Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87