2

Is there a way to stop my console application from scrolling when new text is added? In my application, I am adding new text and I don't want the console to scroll to the bottom each time that happens, I just want it to stay at the top the whole time.

This is for Windows, using the Win API.

user202729
  • 3,358
  • 3
  • 25
  • 36
k0ds
  • 29
  • 3
  • No. There is nothing controlling vertical scrolling in C or C++. You'll have to use platform specific functions or libraries to do that – Ted Lyngmo Dec 18 '21 at 03:03
  • For Windows see [1](https://stackoverflow.com/q/12994296) [2](https://stackoverflow.com/q/3471520) [3](https://stackoverflow.com/q/25432474) for a start. – user202729 Dec 18 '21 at 03:39
  • Which operating system? – user202729 Dec 18 '21 at 03:40
  • @user202729 Those that you sent only show how to remove scrolling, I don't want to do that, I just want the console screen to to stay at the top of the console when new text is added because when new text is added the console screen scrolls to the bottom of that text. This is for Windows. – k0ds Dec 19 '21 at 00:31
  • Maybe take a look at [4](https://stackoverflow.com/questions/60167843/how-to-keep-text-at-the-top-of-console) (although I'm not sure about whether there may be some flicker) [5](https://stackoverflow.com/questions/31740747/system-console-as-a-log-window-with-input-line) or ncurses/other console manipulation libraries. – user202729 Dec 19 '21 at 00:37
  • I think this is possible with [WriteConsoleOutput](https://learn.microsoft.com/en-us/windows/console/writeconsoleoutput), since it doesn't change the cursor position. – ssbssa Dec 20 '21 at 11:38
  • @ssbssa I think ```WriteConsoleOutput()``` might be the way to go, although I am having trouble figuring out how to use it with a ```DWORD``` since the parameters require a ```const void*``` as the characters that will be outputted. – k0ds Dec 21 '21 at 05:14
  • Where do you see that `const void*`? The characters should be in `const CHAR_INFO *`. – ssbssa Dec 21 '21 at 11:36

2 Answers2

0

You can try writing system("cls"); to clear the screen and write from the top again.

  • Note `cls` is specific to Windows – Ranoiaetep Dec 18 '21 at 04:20
  • Ok didn't know that, but they are using Windows. – Lazy Codez Dec 18 '21 at 22:00
  • The OP doesn't want to clear the console just to keep it from scrolling. They want to retain whatever is there. Besides, `system` isn't part of the Windows API. It's [part](https://en.cppreference.com/w/c/program/system) of the C Standard Library. And likely the worst option to ever consider. Launching a process just to clear the console is mighty wasteful. Especially, since there is a dedicated [console API](https://learn.microsoft.com/en-us/windows/console/console-functions) readily available. – IInspectable Dec 19 '21 at 08:41
  • Alright thank you for the info. – Lazy Codez Dec 21 '21 at 04:00
0

If you're using printf

  1. Do not end your lines with \n
  2. Start each line with \r. This will cause the program to continuously overwrite the line where you first started printing
  3. Add a fflush to flush the buffer

If you're using std::cout,

  1. Do not end your lines with std::endl
  2. Start each line wiht \r
  3. Add std::flush at the end of line

The only problem is that if the new line is shorter than the previous line, the text may not be overwritten.

cup
  • 7,589
  • 4
  • 19
  • 42