如何设置标准输出控制台光标位置 [英] How to set console cursor position for stdout

查看:248
本文介绍了如何设置标准输出控制台光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置光标位置控制台屏幕上打印字符。有没有使用的ncurses 库解决方案。

有没有从<一个等效呼吁 SetConsoleCursorPosition 在Linux中href=\"http://$c$cgolf.stackexchange.com/questions/34887/make-a-circle-illusion-animation\">example如下:

 无效打印(INT X,INT Y,焦三){
    COORD P = {X,Y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE)中,p);
    的printf(%C,C);
}


解决方案

也许一点点的历史是为了。你所谓的Windows中的控制台确实是一个终端仿真器的一个例子。然而,不同于传统的终端仿真器,Windows提供了通过其API控制台终端直接访问。通过终端(因此终端仿真器)所采取的传统的办法是间preT的转义序列的,每一个指示终端执行某些操作。

不幸的是,你可能想象,终端的能力差别很大(有的甚至可以得出这样或那样的图形),所以并不是所有的终端使用相同的一组转义序列。这些天,你会很倒霉遇到一些不执行 ANSI / VT100逃脱的一个超集;如果你很高兴只支持ANSI / VT100和衍生物,您可以发送 ESC [N;月时,其中的 N 的是行号, M 的列数。

然而,如果你的的做到这一点,你的code不会,如果它与任何异国psented $ P $工作。在这种情况下,你真的应该考虑使用curses库(ncurses的是一个共同的,但不是唯一的例子),即使你只使用它来提取terminfo数据库(信息虽然我强烈建议只使用诅咒库)。

最后,注:code从Windows你引用不一定会工作,为什么(!)?因为的printf()做缓冲输出,而且也不能保证您再次更改光标的位置之前,你的角色将被发送到控制台。您可以通过使用修复这个 fflush(标准输出),但说实话,如果你使用 SetConsoleCursorPosition 你可能也只是使用 WriteConsole ,并用它做。

其他有用的信息

其实你可以得到一个版本的Windows上运行的咒骂;例如见 PDCurses 。如果你使用的诅咒,那么,你就不需要任何针对Windows的code两种,的你在任何终端在使用工作。

I want to set cursor position to print a char on console screen. Is there a solution without using ncurses library.

Is there an equivalent call for SetConsoleCursorPosition in Linux from example below:

void print (int x, int y, char c) {
    COORD p = { x, y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p);
    printf("%c", c);
}

解决方案

Perhaps a little history is in order. What you call the "console" in Windows is really an example of a terminal emulator; however, unlike traditional terminal emulators, Windows provides direct access to the terminal via its Console API. The traditional approach taken by terminals (and hence terminal emulators) was to interpret escape sequences, each of which instructs the terminal to perform some operation.

Unfortunately, as you might imagine, terminals' capabilities varied widely (some could even draw graphics of one sort or another), and so not all terminals use the same set of escape sequences. These days you'd be very unlucky to encounter something that didn't implement a superset of the ANSI/VT100 escapes; if you're happy to only support ANSI/VT100 and derivatives, you could send ESC [ n ; m H, where n is the row number and m the column number.

However, if you do do that, your code won't work if it's presented with anything exotic. In that case, you really should consider using a curses library (ncurses being a common but not the only example), even if you're only using it to extract information from the terminfo database (though I'd strongly recommend just using the curses library).

Finally, a note: the code you quote from Windows won't necessarily work(!) Why? Because printf() does buffered output, and there's no guarantee that your character will be sent to the console before you change the cursor position again. You could fix this by using fflush(stdout), but honestly if you're using SetConsoleCursorPosition you may as well just use WriteConsole and be done with it.

Additional useful information

You can actually get a version of curses that runs on Windows; see e.g. PDCurses. If you were to use curses, then, you wouldn't need any Windows-specific code either, and you'd work on whatever terminal was in use.

这篇关于如何设置标准输出控制台光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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