如何在C中定位输入文本光标? [英] How to position the input text cursor in C?

查看:42
本文介绍了如何在C中定位输入文本光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我有一个非常简单的程序:

Here I have a very simple program:

 printf("Enter your number in the box below
");
 scanf("%d",&number);

现在,我希望输出如下所示:

Now, I would like the output to look like this:

 Enter your number in the box below
 +-----------------+
 | |*|             |
 +-----------------+

哪里,|*|是用户输入值的闪烁光标.

Where, |*| is the blinking cursor where the user types their value.

由于C是线性代码,它不会打印box art,然后要求输出,它会打印顶行和左列,然后在输入后打印底行和右列.

Since C is a linear code, it won't print the box art, then ask for the output, it will print the top row and the left column, then after the input print the bottom row and right column.

那么,我的问题是,我可以先打印盒子,然后让函数将光标移回盒子吗?

So, my question is, could I possibly print the box first, then have a function take the cursor back into the box?

推荐答案

如果你在某个 Unix 终端下(xterm, gnome-terminal ...),你可以使用控制台代码:

If you are under some Unix terminal (xterm, gnome-terminal ...), you can use console codes:

#include <stdio.h>

#define clear() printf("33[H33[J")
#define gotoxy(x,y) printf("33[%d;%dH", (y), (x))

int main(void)
{
    int number;

    clear();
    printf(
        "Enter your number in the box below
"
        "+-----------------+
"
        "|                 |
"
        "+-----------------+
"
    );
    gotoxy(2, 3);
    scanf("%d", &number);
    return 0;
}

或者使用框绘图字符:

printf(
    "Enter your number in the box below
"
    "╔═════════════════╗
"
    "║                 ║
"
    "╚═════════════════╝
"
);

更多信息:

man console_codes

这篇关于如何在C中定位输入文本光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆