Continuing from the comments above, if you are outputting to a VT compatible terminal (most are), you can use ANSI Escape sequences to control clearing to end-of-line (and various other cursor movements and visibility).
A short example working with lines terminated with a carriage-return, you can use 33[K
to clear to end of line. (the formal number preceding 'K'
is "0K"
but can be omitted due to it being the default) The other line clearing options are "1K"
clear to beginning of line, and "2K"
clear entire line. You can also hide and restore the cursor with similar escapes.
In your case you can handle the additional characters from previous longer lines as:
#include <iostream>
#include <unistd.h>
int main (void) {
const char *lines[] = { "You do not ever want
",
"your favorite dog
",
"to have
",
"fleas!
" };
int n = sizeof lines / sizeof *lines;
std::cout << "33[?25l"; /* hide cursor */
for (int i = 0; i < n; i++) {
std::cout << "33[0K" << lines[i]; /* clear to end, output line */
std::fflush (stdout);
sleep (2);
}
std::cout << "33[?25h
"; /* restore cursor */
}
Compile normally and run in your terminal. If it is VT compatible it will process the escapes so you see each line printed without any of the additional characters from the preceding line interfering with the output.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…