gotoxy()实现为Linux用printf [英] gotoxy() implementation for Linux using printf
问题描述
我一直在寻找gotoxy()的gcc编译器的替代品,发现这个 -
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);
}
我想知道它是如何运作的,我的意思是,我们使用时, [
和;
里面的printf,是什么 0x1B
在那里做什么,它是如何把光标移动到这个code行/列?
I want to know how is it functioning, I mean when do we use [
and ;
inside printf, what is 0x1B
doing there and how does it take the cursor to the row/column with this code?
我从来没有见过这种做法在书本printf的,所以这将是巨大的,如果你能指出我一个链接,我可以找到有关的printf等用途。
I have never seen this type of practice for printf in books,so it would be great If you could point me to a link where I can find about such uses of printf.
推荐答案
这是使用终端逃避codeS 来定位光标。
\\ X1B
是转义字符,它告诉你的终端,它下一步怎么走是不是意味着在屏幕上打印,而是一个命令到终端(或者最有可能的终端仿真器)
"\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)
尾部的F表示要强制某处光标位置,用坐标表示,precede它。
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
所以,如果你叫 gotoxy(4,2)
,它结束了发送转义序列(ESC)[2; 4F
,其中 ESC
是字节 0x1B
。
So if you call gotoxy(4,2)
, it ends up sending the escape sequence "(ESC)[2;4f"
where ESC
is the byte 0x1B
.
这篇关于gotoxy()实现为Linux用printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!