繁体   English   中英

如何从屏幕右侧而不是 C 中通常的左侧按 output 进行打印?

[英]How to print by output from the right hand side of the screen instead of the usual left hand side in C?

我正在编写一个游戏,我需要在其中创建一些块。 我对该部分进行了完美编程,但问题是它们需要与屏幕的右侧对齐,而不是通常的左侧。 现在,我知道打印空白的漫长方法,但我只是好奇是否有任何快捷方式可以从 C 或 C++ 的右侧打印 output?

如果要引用类似于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>

哦,请注意,要正确对齐,我相信您必须设置宽度。

希望能帮助到你。

首先,找出屏幕上有多少列。 这取决于您要为其编程的平台。 我没有帮助您,因为您没有指定平台。

对于实际的打印,您可以将printf字段宽度说明符配合使用,该说明符会将您的文本右对齐到给定的字段宽度。

对于更复杂的情况,请查看curses,这是一个用于终端编程的综合库。

在最常见的类似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;
}

如其他建议的答案所述,解决方案实际上取决于程序在其上运行的系统的类型,更重要的是取决于显示结果的设备的类型。 使用格式控制(如printf或cout)的对齐方式的变化与使用空白填充自己的输出行基本相同,但使用的是更优雅的编程界面。

越过那些(因为它们似乎不是所请求的),因此显示设备的类型是令人关注的。 图形显示普遍允许人们将文本放置在(可编程)设备上的任何位置。 但是,字符单元设备(例如终端)使其变得更难一点。 您可能在POSIX系统上使用的任何工具都允许您在光标位置写文本,并使用cursor-addressing更改您写文本的位置。 (Windows控制台提供了类似的界面,但具有不同的细节-由于未指定系统,因此大多数人都认为POSIX是必需的)。

使用游标寻址,您可以通过以下操作编写一个在屏幕右侧对齐的给定字符串:

  • 找到屏幕的宽度,称为W
  • 找到字符串的长度,调用L (实际上,您需要它在屏幕上使用的单元格数量-以字节为单位的UTF-8字符串的长度与其宽度不同)。
  • 光标移动到屏幕当前行上的单元格W - L (从零开始计数)。
  • 在屏幕上写文字

尽管不是POSIX的一部分,但TIOCGWINSZ功能得到了广泛支持,并提供了一种获取屏幕宽度的方法,例如, 如何设置终端的尺寸? (某些系统使用TIOCGSIZE符号支持类似的调用,如在C获取终端宽度中所述 )。

与其通过写空格沿行移动光标,不如在控制序列中选择更少的字符。 要移动光标,有以下选择:

  • 硬编码诸如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, WL); addstr(mystring);

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

假设我们正在尝试从右侧打印“*”。我们总共要打印 10 颗星。为了更好地可视化,我将使用 Sleep(1000) 来查看正在打印的星星。这是示例程序将如下所示:

#include<iostream>
#include<iomanip>
#include<Windows.h>

using namespace std;

int main() {    
    
    
    int n=10;
    for(int i=n;i>0;i--){

        cout<<setw(i+1)<<"*\r"<<flush;
        Sleep(1000);
    }
    return 0;
   
}

这里使用“iomanip”库中的 setw() function 来设置要打印的字符串的宽度。“i+1”中的附加“+1”用于说明字符“\r”,即c++ 中的转义字符称为“回车”,它告诉终端仿真器将 cursor 移动到行的开头,而不是下一行,例如 \n。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM