How to set console cursor position for stdout

12,970

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.

Share:
12,970
manav m-n
Author by

manav m-n

Please note that this profile is outdated and kept here only for historical reasons. Name: Unknown Location: Unknown Education: Unknown Facebook/Twitter/Instagram: None

Updated on June 07, 2022

Comments

  • manav m-n
    manav m-n almost 2 years

    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 call for SetConsoleCursorPosition in Linux from example below:

    void print (int x, int y, char c) {
        COORD p = { x, y };
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p);
        printf("%c", c);
    }
    
  • manav m-n
    manav m-n almost 10 years
    can we use struct termios from termios.h and its API's instead of using the ESC sequence. I want to know the low level Linux calls for getting current cursor position.
  • al45tair
    al45tair almost 10 years
    @Manav There aren't any low-level Linux calls for getting the current cursor position. You need to use escape sequences. Why? Because only the terminal (which might be a separate piece of hardware) knows where the cursor is. You need to send it an escape sequence to ask, and it will reply with the answer. To get the cursor position, send ESC [ 6 n, then read the reply (which will be an ESC [ n ; m R sequence containing the cursor location). But seriously, USE CURSES! It's easier and better.