簡體   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