In other words, your program has already written something to standard output and now you want to do something for the characters that "have already left the building"?
There is a fundamental problem: you don't know what is "out there".
Lets say that the name of your program is
foo.exe. You might think that the user runs it in a shell:
The shell hands out the output to terminal window, which draws something to the screen.
That case has some solutions. The problem is that different terminals do not have common API. There are third-party libraries like
ncurses that your program can talk to and who can talk to several terminal types.
However, I could run your program:
or
while read LINE ; do echo ${LINE} ; done < <(foo.exe | sort) |
In neither case does the output appear on the terminal screen as such. If your program nevertheless "wipes the screen", it will not wipe what it wrote out a moment earlier but something else that happened to be on the screen.
Anyway. Seek out the ncurses. It or a variant of it is probably available for your platform.