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

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

问题描述

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

Here I have a very simple program:

 printf("Enter your number in the box below\n");
 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是线性代码,因此它不会打印美术字,而是要求输出,它将打印上一行和左列,然后在输入之后打印下一行和右列.

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终端(xtermgnome-terminal ...),则可以使用控制台代码:

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

#include <stdio.h>

#define clear() printf("\033[H\033[J")
#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))

int main(void)
{
    int number;

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

或使用框画字符:

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

更多信息:

man console_codes

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

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