Вопрос

Sometimes, when I install programs, I see progress bar in console window (or other, dynamic changing text). It is displayed, and changing in the same row of string. When I use printf or other function, I write next text, but I can't modify in console window already existing text. How can I do it through C?

Это было полезно?

Решение

The easiest way for this kind of effects is to use the caret return \r character that lets you write over the previous line.


If you'd like to have direct access to the console's buffer (i.e. writing in specific X/Y position instead of line-by-line), then you're probably looking for a helper library like pdcurses.

Другие советы

If you want to keep using printf and have an easy go at "changing" the text, use \r which is carriage return on linux. It won't work on MAC though, because it means newline.

If you do this, I'd advise using ioctl to hide the caret to prevent flickering and an ugly effet.

If you want a better way of manipulating text on-screen you can have a look at ncurses

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top