1

I need to get my (Y, X) position of my terminal's cursor with the termcaps in C.

With ioctl() i get the size of my terminal screen, but i still need to get where my cursor is.

I found this :

CSI 6 n DSR – Device Status Report Reports the cursor position to the application as (as though typed at the keyboard) ESC[n;mR, where n is the row and m is the column. (May not work on MS-DOS.)

But i have no idea how to use it in C ...

albttx
  • 3,444
  • 4
  • 23
  • 42

2 Answers2

4

You write the command sequence (<ESC>[6n) to the terminal using normal output to stdout. Then you read the response using normal input from stdin.

You need to parse the "reply" to pick out the position.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
-3

here is a gotoxy() function, that can print on specified x and y position in gcc linux

#include<stdio.h>
//gotoxy function
void gotoxy(int x,int y)
{
printf("%c[%d;%df",0x1B,y,x);
}
main ()
{
gotoxy(25,50); //reposition cursor
printf("hello world"); //display text
}

here is a reference for console input and output , if you are using terminal for windows (dos prompt)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Mike
  • 169
  • 10