John Smith
John Smith

Reputation: 3

Get console's cursor position

I am kinda new to C++ and i was wondering if is there any way to get the Console's Cursor position (x, y)?

For example, In C# i can use:

Console.CursorLeft for X and Console.CursorTop for Y

Upvotes: 0

Views: 1433

Answers (1)

catnip
catnip

Reputation: 25388

If you are on Windows, you can do this (error handling omitted for brevity):

HANDLE hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi = { };
BOOL ok = GetConsoleScreenBufferInfo (hConsoleOutput, &csbi);

The current cursor position should then be in csbi.dwCursorPosition;

Documentation here and here.

On Unix platforms, you would use the ncurses library.

Upvotes: 2

Related Questions