我正在编写一个我需要创建一些块的游戏。我完美地编写了那个部分,但问题是它们需要朝向屏幕的右侧对齐,而不是通常的左侧。现在,我知道打印空白的漫长方法,但我很好奇是否有任何快捷方式可以在C或C ++中打印右边的输出?
答案 0 :(得分:2)
如果您指的是类似于Console stdout(C ++)的内容,例如COUT, 你可以使用这个:
http://www.cplusplus.com/reference/ios/right/
或iomanip库允许您具有许多文本格式功能。 例如:
cout << setw(20) << setiosflags(ios::right) << "Hello World!" << endl;
http://www.cplusplus.com/reference/iomanip/
不要忘记#include <iomanip>
。
哦,请注意,为了正确对齐,我相信你必须设置宽度。
希望它有所帮助。
答案 1 :(得分:0)
首先,弄清楚屏幕有多少列。这取决于您编程的平台;我无法帮助你,因为你没有指定平台。
对于实际打印,您可以将printf
与 field-width 说明符一起使用,该说明符将文本右对齐到给定的字段宽度。
对于更复杂的案例,请查看curses,一个用于终端编程的综合库。
答案 2 :(得分:0)
在大多数常见的Unix平台上,您可以使用ioctl
系统调用:
#include <sys/ioctl.h>
#include <stdio.h>
int main()
{
char *string = "Hello World";
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
printf("%*s\n", w.ws_col, string);
return 0;
}
答案 3 :(得分:0)
正如其他建议的答案所述,解决方案实际上取决于程序运行的系统类型,更重要的是取决于显示结果的设备类型。使用格式化控件进行对齐的变化(与printf或cout一样)与使用空白填充输出行基本相同,但使用更优雅的编程接口。
移过那些(因为它们似乎不是所要求的),显示设备的类型是有意义的。图形显示通常允许用户将文本放置在(可编程)设备上的任何位置。然而,诸如终端之类的字符单元设备使其变得更难。您可能在POSIX系统上使用的任何内容都允许您在光标位置写入文本,并使用 cursor-addressing 更改您写入文本的位置。 (Windows控制台提供了一个类似的界面,具有不同的细节 - 因为没有指定系统,POSIX是大多数人所认为的。)
使用光标寻址,您可以通过这样做来编写在屏幕右侧对齐的给定字符串:
W
。L
(实际上你需要在屏幕上使用 cells 的数量 - length UTF-8字符串的字节数与 width 的不同。W - L
(从零开始计算)。虽然不属于POSIX,但广泛支持TIOCGWINSZ
功能,并提供了获取屏幕宽度的方法,例如 How to set the terminal's size? 。 (某些系统支持使用TIOCGSIZE
符号进行类似的调用,如 Getting terminal width in C? 中所述。
不是通过写空格沿着线移动光标,而是可以在control sequence中选择更少的字符。要移动光标,可以选择:
HPA
(水平位置,绝对值)之类的控制序列进行硬编码。不建议使用文档,例如,在Linux的 console_codes 手册页或 XTerm Control Sequences 中。这个序列不在VT100中,许多终端仿真器都是基于此,但在ISO-6429中有记录。 XTerm在1997中添加了它(在Linux中没有那个时代的文档)。使用termcap询问终端是否支持HPA
(在termcaps中称为"ch"
)或使用带有参数的CUF
(光标前进)(但这需要你知道你的位置)。假设终端支持HPA
,您的程序将执行类似
char *hpa = tgetstr("cm", &areap);
tgoto(hpa, W - L, 0);
puts(mystring);
使用curses,让它决定如何前往正确的地方:
int y, x;
getyx(stdscr, y, x);
move(y, W-L);
addstr(mystring);
答案 4 :(得分:0)
the following, some of the info found at: <http://wiki.bash-hackers.org/scripting/terminalcodes>
should greatly help you with handling the screen/cursor activities.
General useful ASCII codes
The Ctrl-Key representation is simply associating the non-printable characters from ASCII code 1 with the printable (letter) characters from ASCII code 65 ("A"). ASCII code 1 would be ^A (Ctrl-A), while ASCII code 7 (BEL) would be ^G (Ctrl-G). This is a common representation (and input method) and historically comes from one of the VT series of terminals.
Name decimal octal hex C-escape Ctrl-Key Description
BEL 7 007 0x07 \a ^G Terminal bell
BS 8 010 0x08 \b ^H Backspace
HT 9 011 0x09 \t ^I Horizontal TAB
LF 10 012 0x0A \n ^J Linefeed (newline)
VT 11 013 0x0B \v ^K Vertical TAB
FF 12 014 0x0C \f ^L Formfeed (also: New page NP)
CR 13 015 0x0D \r ^M Carriage return
ESC 27 033 0x1B <none> ^[ Escape character
DEL 127 177 0x7F <none> <none> Delete character
Cursor handling
ANSI terminfo equivalent Description
[ <X> ; <Y> H
[ <X> ; <Y> f cup <X> <Y> Home-positioning to X and Y coordinates
:!: it seems that ANSI takes 1-1 as root while tput takes 0-0
[ H home Home-positioning to root (0-0)
7 sc Save current cursor position
8 rc Restore current cursor position
:?: most likely a normal code like \b cub1 move left one space (backspace)
VT100 [ ? 25 l civis switch cursor invisible
VT100 [ ? 25 h cvvis switch cursor visible
Erasing text
ANSI terminfo equivalent Description
[ K
[ 0 K el Clear line from current cursor position to end of line
[ 1 K el1 Clear line from beginning to current cursor position
[ 2 K el2:?: Clear whole line (cursor position unchanged)
General text attributes
ANSI terminfo equivalent Description
[ 0 m sgr0 Reset all attributes
[ 1 m bold Set "bright" attribute
[ 2 m dim Set "dim" attribute
[ 4 m set smul unset rmul :?: Set "underscore" (underlined text) attribute
[ 5 m blink Set "blink" attribute
[ 7 m rev Set "reverse" attribute
[ 8 m invis Set "hidden" attribute
Foreground coloring
ANSI terminfo equivalent Description
[ 3 0 m setaf 0 Set foreground to color #0 - black
[ 3 1 m setaf 1 Set foreground to color #1 - red
[ 3 2 m setaf 2 Set foreground to color #2 - green
[ 3 3 m setaf 3 Set foreground to color #3 - yellow
[ 3 4 m setaf 4 Set foreground to color #4 - blue
[ 3 5 m setaf 5 Set foreground to color #5 - magenta
[ 3 6 m setaf 6 Set foreground to color #6 - cyan
[ 3 7 m setaf 7 Set foreground to color #7 - white
[ 3 9 m setaf 9 Set default color as foreground color
Background coloring
ANSI terminfo equivalent Description
[ 4 0 m setab 0 Set background to color #0 - black
[ 4 1 m setab 1 Set background to color #1 - red
[ 4 2 m setab 2 Set background to color #2 - green
[ 4 3 m setab 3 Set background to color #3 - yellow
[ 4 4 m setab 4 Set background to color #4 - blue
[ 4 5 m setab 5 Set background to color #5 - magenta
[ 4 6 m setab 6 Set background to color #6 - cyan
[ 4 7 m setab 7 Set background to color #7 - white
[ 4 9 m setaf 9 Set default color as background color
Misc codes
Save/restore screen
Used capabilities: smcup, rmcup
You've undoubtedly already encountered programs that restore the terminal contents after they do their work (like vim). This can be done by the following commands:
# save, clear screen
tput smcup
clear
# example "application" follows...
read -n1 -p "Press any key to continue..."
# example "application" ends here
# restore
tput rmcup
These features require that certain capabilities exist in your termcap/terminfo. While xterm and most of its clones (rxvt, urxvt, etc) will support the instructions, your operating system may not include references to them in its default xterm profile. (FreeBSD, in particular, falls into this category.) If `tput smcup` appears to do nothing for you, and you don't want to modify your system termcap/terminfo data, and you KNOW that you are using a compatible xterm application, the following may be work for you:
echo -e '\033[?47h' # save screen
echo -e '\033[?47l' # restore screen
The following is more specific to cursor placement:
<http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html>
- Position the Cursor:
\033[<L>;<C>H
Or
\033[<L>;<C>f
puts the cursor at line L and column C.
- Move the cursor up N lines:
\033[<N>A
- Move the cursor down N lines:
\033[<N>B
- Move the cursor forward N columns:
\033[<N>C
- Move the cursor backward N columns:
\033[<N>D
- Clear the screen, move to (0,0):
\033[2J
- Erase to end of line:
\033[K
- Save cursor position:
\033[s
- Restore cursor position:
\033[u