tags:

views:

60

answers:

4

I am writing a C program which is to be executed on the Linux terminal. The program goes into an infinite loop and prints five lines over and over again. How do I get the cursor back to the previous lines?

E.g. I want to print the alphabets and replace them every 15 seconds. So, at T=0, output is

sh>./a.out
AA
BB
CC
DD
EE

At T=15, output is

sh>./a.out
FF
GG
HH
II
JJ

I tried to use lseek over STDOUT to make it overwrite the previous text. But I guess the terminal does not support lseek. Do I have to tinker with the driver APIs? Or is there a simpler way to do that?

+2  A: 

See curses.

Jerry Coffin
+2  A: 

You need a curses library, such as ncurses.

Mark Ransom
+1  A: 

There is no easy way to do what you want. Think of stdout as a continuous sheet of paper that is impossible to pull back. Once you print a line, that's it. No more changes to that line.

You can "transform stdout" to a different kind of printer, by using specific libraries (curses is common) not defined by the Standard.

pmg
+1  A: 

Running in a Linux terminal, you should be able to use the '\r' character which is a carriage return (without the new line). It will overwrite what was there before.

Try something like :

#include <stdio.h>

int main(void)
{
    printf("AA BB CC");
    fflush(stdout);
    sleep(3);
    printf("\rDD EE FF");
    fflush(stdout);
    sleep(3);
    printf("\n");

    return 0;
}

With that, you should be able to do whatever you want in your loop...

Edit... using ncurses :

#include <stdio.h>
#include <ncurses.h>

int main(void)
{

    initscr();
    noecho();
    raw();

    printw("AA\nBB\nCC\n");
    refresh();
    sleep(3);
    mvwprintw(stdscr, 0, 0, "DD\nEE\nFF\n");
    refresh();
    sleep(3);

    endwin();

    return 0;
}
Matthieu
That will only work if what you are trying to change is all on the same line as was the initial question...
Matthieu
But it's not.... AA and BB are in consecutive lines. It outputted AA BB CC DD EE in the same line.
Confused coder
Then curses or ncurses like the other people answered...
Matthieu
+1 for `ncurses` and a real example. Note that in this specific and fairly simple case, one could look up the terminal control sequences for Home Cursor, Clear Screen and/or GOTO XY and get away with it since nearly all consoles pretend to be a VT100 or vaguely compatible. Back in the day, however, without `termpcap` and the `curses` library on top of it, it was a royal pain to be compatible with all the wacky terminals out there.
RBerteig
Trivia for historical context: terminals used to print on paper. The platen roller could only be driven in one direction, and the paper would scroll out of the top of the terminal and collect in heaps on the floor (the wire basket things never worked). CRT based terminals came later and were known as "glass teletypes". They were *less* useful because you couldn't scroll back to see what you'd typed that morning. Eventually they got smarter....
RBerteig