0

How can we erase only 1 output on the screen? (to clearscreen we use system("cls") but if we whant to clear only 1 line how?)thx

16th Oct 2018, 12:00 PM
Jihad AL AKL
Jihad AL AKL - avatar
2 Respuestas
+ 1
On windows you would have to get the handle to the console window, then ask windows for the console info. This info contains things like the X and Y dimentions for the console. The X dimension is the important one here, as it tells you how much to delete on said line. Then you simply fill the console with a character using another function. Here is an example code, I recommend typing it yourself instead of copying it: #include <iostream> #include <windows.h> void setCursor( COORD cursor ) { SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ) , cursor ); } void clearScreen( COORD startPoint = { 0, 0 } ) { // Struct for the console info CONSOLE_SCREEN_BUFFER_INFO info; // Handle to console HANDLE console = GetStdHandle( STD_OUTPUT_HANDLE ); // Get the console info GetConsoleScreenBufferInfo( console, &info ); // Not used, but needed for the following 2 functions DWORD written; // Fills the console // 1st argument is the console handle // 2nd argument is the character to fill the console with // 3rd argument is how much to fill // 4th argument is where to start filling // 5th argument is used to store how many characters were written ( probably won't use it, but needed anyway ) FillConsoleOutputCharacter( console, ' ', info.dwSize.X, startPoint, &written ); // Same for this one, except the 2nd argument is used to clear any use of color FillConsoleOutputAttribute( console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE, info.dwSize.X, startPoint, &written ); // Restore the cursor setCursor( startPoint ); } int main() { int x = 0; for( int i = 0; i < 5000; ++i ) std::cout << char( i%95+32 ); while( 1 ) { COORD line{ 0, x++ }; setCursor( line ); Sleep( 200 ); clearScreen( line ); } } Now, with this code, only a minor modification is needed to make it clear the whole screen, can you figure it out?
16th Oct 2018, 12:44 PM
Dennis
Dennis - avatar
0
I ran out of space in the previous post... Alternativaly you can use the '\r' escape character to go back to the beginning of the line. But this doesn't override the line, you would have to keep track of how many characters you displayed and then print the same number of spaces, unless the next output is guaranteed to be of equal length or longer, nor does it allow you to go back to a previous line.
16th Oct 2018, 12:55 PM
Dennis
Dennis - avatar