【问题标题】:How to print to a desired screen location in C如何在 C 中打印到所需的屏幕位置
【发布时间】:2017-05-11 03:46:30
【问题描述】:

在使用 C 编码并将输出打印到屏幕时,我们可以不使用\t\t\t 打印到屏幕上的不同位置吗?该位置应由某种坐标指定。

例如printf("hallo hai")

上面的打印信息会出现在我们想要的地方。它不应该使用\t\t\t 而是放置在数字表示的位置。

【问题讨论】:

  • 你需要改写你的问题,我怀疑有人会明白你的意思。
  • 什么? \t\t\t
  • 应用 detab 和输出?
  • 查看ncurses
  • 您可以计算出您需要的空格数并先打印出来。但是,如果您想将光标定位在特定坐标上,那么这将取决于您的控制台如何处理事情(这不是 C 的事情,但取决于系统的其他部分)。尝试使用 ncurses 之类的终端控件库,或打印 ANSI 终端控件转义序列。

标签: c


【解决方案1】:

我建议您使用 curses 或其更新 ncurses。

以下代码使用 ncurses 打印 hello world:

/*
  "Hello, world!", ncurses style.
*/


#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>                  /*  for sleep()  */
#include <curses.h>


int main(void) {

    WINDOW * mainwin;


    /*  Initialize ncurses  */

    if ( (mainwin = initscr()) == NULL ) {
    fprintf(stderr, "Error initialising ncurses.\n");
    exit(EXIT_FAILURE);
    }


    /*  Display "Hello, world!" in the centre of the
    screen, call refresh() to show our changes, and
    sleep() for a few seconds to get the full screen effect  */

    mvaddstr(13, 33, "Hello, world!");
    refresh();
    sleep(3);


    /*  Clean up after ourselves  */

    delwin(mainwin);
    endwin();
    refresh();

    return EXIT_SUCCESS;
}

【讨论】:

    猜你喜欢
    • 2020-01-20
    • 2019-04-13
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多