I want to set cursor position to print a char
on console screen. Is there a solution without using ncurses
library.
Is there an equivalent
Perhaps a little history is in order. What you call the “console” in Windows is really an example of a terminal emulator; however, unlike traditional terminal emulators, Windows provides direct access to the terminal via its Console API. The traditional approach taken by terminals (and hence terminal emulators) was to interpret escape sequences, each of which instructs the terminal to perform some operation.
Unfortunately, as you might imagine, terminals' capabilities varied widely (some could even draw graphics of one sort or another), and so not all terminals use the same set of escape sequences. These days you'd be very unlucky to encounter something that didn't implement a superset of the ANSI/VT100 escapes; if you're happy to only support ANSI/VT100 and derivatives, you could send ESC [ n ; m H
, where n is the row number and m the column number.
However, if you do do that, your code won't work if it's presented with anything exotic. In that case, you really should consider using a curses library (ncurses being a common but not the only example), even if you're only using it to extract information from the terminfo database (though I'd strongly recommend just using the curses library).
Finally, a note: the code you quote from Windows won't necessarily work(!) Why? Because printf()
does buffered output, and there's no guarantee that your character will be sent to the console before you change the cursor position again. You could fix this by using fflush(stdout)
, but honestly if you're using SetConsoleCursorPosition
you may as well just use WriteConsole
and be done with it.
Additional useful information
You can actually get a version of curses that runs on Windows; see e.g. PDCurses. If you were to use curses, then, you wouldn't need any Windows-specific code either, and you'd work on whatever terminal was in use.