gotoxy() implementation for Linux using printf

前端 未结 3 1121
逝去的感伤
逝去的感伤 2020-12-09 11:43

I was looking for a substitute of gotoxy() for gcc compiler and found this -

void gotoxy(int x,int y)
{
    printf(\"%c[%d;%df\",0x1B,y,x);
}
相关标签:
3条回答
  • 2020-12-09 12:00

    That is known as an ANSI escape code. I haven't seen those used in a while. Here's a page that explains about them:

    http://en.wikipedia.org/wiki/ANSI_escape_code

    0 讨论(0)
  • 2020-12-09 12:01

    These are called ANSI Escape Sequences and are derived from the DEC VT100 display terminal which was introduced in 1978.

    0 讨论(0)
  • 2020-12-09 12:16

    This is using terminal escape codes to position the cursor.

    "\x1B" is the escape character that tells your terminal that what comes next is not meant to be printed on the screen, but rather a command to the terminal (or most likely terminal emulator)

    The trailing 'f' indicates that you want to force the cursor position somewhere, indicated by the coordinates that precede it.

    Force Cursor Position   <ESC>[{ROW};{COLUMN}f
    

    So if you call gotoxy(4,2), it ends up sending the escape sequence "(ESC)[2;4f" where ESC is the byte 0x1B.

    0 讨论(0)
提交回复
热议问题