如何在 C++ 中的特定坐标处将字符串打印到控制台?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1670891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I print a string to the console at specific coordinates in C++?
提问by Shawn
I'm trying to print characters in the console at specified coordinates. Up to now I have been using the very ugly printf("\033[%d;%dH%s\n", 2, 2, "str");
But I just had to ask whether C++ had any other way of doing this. The problem is not even that it's ugly, the problem comes up when I try to make myself a prettier function like so:
我正在尝试在控制台中的指定坐标处打印字符。到目前为止,我一直在使用非常丑陋的printf("\033[%d;%dH%s\n", 2, 2, "str");
但我只需要问 C++ 是否有其他方法可以做到这一点。问题甚至不在于它丑陋,当我试图让自己成为一个更漂亮的函数时,问题就出现了:
void printToCoordinates(int x, int y, string text)
{
printf("3[%d;%dH%s\n", x, x, text);
}
It doesn't work, even if I typecast to (char*)
.
Another problem is that I have to print out the \n
for the page to be refreshed... I just don't enjoy using printf
in general.
它不起作用,即使我将类型转换为(char*)
. 另一个问题是我必须打印出来\n
才能刷新页面...我只是不喜欢使用printf
一般。
Similarily to using cout
instead of printf
, I believe there should be a more recent way of doing this (ideally a way that allows me to easily write strings where I want on the screen, and ideally a way that doesn't required these weird symbols: \033[%d;%dH
)
以相若方式使用cout
,而不是printf
,我认为应该有一个更近的这样的方式(最好的方式,让我去,我想在屏幕上轻松写字符串,最好是不要求这些奇怪的符号的方式:\033[%d;%dH
)
So, do any of you have what I'm looking for?
那么,你们中有人有我要找的东西吗?
采纳答案by CB Bailey
What you are doing is using some very terminal specific magic characters in an otherwise pure C++ application. While this works, you will probably have a far easier time using a library which abstracts you from having to deal with terminal specific implementation details and provides functions that do what you need.
您正在做的是在其他纯 C++ 应用程序中使用一些非常终端特定的魔法字符。虽然这有效,但您可能会更轻松地使用一个库,该库使您不必处理终端特定的实现细节并提供执行您需要的功能。
Investigate whether curses or ncurses libraries are available for your system.
调查curses 或ncurses 库是否可用于您的系统。
回答by Jacob
I remember using gotoxy(x,y)
in Turbo C++ (conio.h) - don't know if it'll work for you though. It moves the cursor to the coordinates specified by x
and y
.
我记得gotoxy(x,y)
在 Turbo C++ (conio.h) 中使用过 - 不过不知道它是否适合你。它将光标移动到由x
和指定的坐标y
。
EDIT:If you're using Windows, here's a gotoxy
clone:
编辑:如果您使用的是 Windows,这里有一个gotoxy
克隆:
#include <windows.h>
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
回答by Greg Hewgill
A few improvements to your function:
对您的功能的一些改进:
void printToCoordinates(int x, int y, const char *format, ...)
{
va_list args;
va_start(args, format);
printf("3[%d;%dH", x, y);
vprintf(format, args);
va_end(args);
fflush(stdout);
}
This version:
这个版本:
- allows you to use any arbitrary format string and variable argument lists
- automatically flushes
stdout
without printing a newline - uses
x
andy
in the format string (your use ofx
andx
may have been a typo)
- 允许您使用任意格式的字符串和变量参数列表
- 自动刷新
stdout
而不打印换行符 - 在格式字符串中使用
x
和y
(您使用x
和x
可能是一个错字)
However, because varargs is essentially a C feature and doesn't really understand C++ objects, you'd have to call it like this:
然而,因为 varargs 本质上是一个 C 特性并且并不真正理解 C++ 对象,所以你必须像这样调用它:
printToCoordinates(10, 10, "%s", text.c_str());
A better option really is to use curses (for Unix-like platforms) or Win32 console functions (for Windows) as mentioned in other answers.
更好的选择确实是使用其他答案中提到的curses(对于类Unix平台)或Win32控制台函数(对于Windows)。
回答by sbi
First:
第一的:
void printToCoordinates(int x, int y, string text)
{
printf("3[%d;%dH%s\n", x, x, text);
}
You don't want to copy the string argument, you want to pass it by (const
) reference. Also, the (only) right way to get a char*
from a std::string
is to call its c_str()
member function:
您不想复制字符串参数,而是想通过 ( const
) 引用传递它。此外,char*
从 a获取 a 的(唯一)正确方法std::string
是调用其c_str()
成员函数:
void printToCoordinates(int x, int y, const std::string& text)
{
printf("3[%d;%dH%s\n", x, x, text.c_str());
}
As to your question: C++ has no way to do what you want, but it allows you to use platform-specific ways to do it. You would have to tell us your platform in order to get meaningful answers.
至于你的问题:C++ 没有办法做你想做的事,但它允许你使用特定于平台的方式来做到这一点。您必须告诉我们您的平台才能获得有意义的答案。
回答by shrinjoy biswas
void screenpos(int x,int y,char textyowanawrite[20])
{
//printf for right shift
// \n for downward shift
//loops through the rows and shifts down
for(int row=0;row<=y;row++)
{
printf("\n");
for (int i = 0; i < x; i++)
{
printf("%s "," " );
}
}
printf("%s ",textyowanawrite );
}
//this should work to certain extinct only problem is u cant go from somewhere like 4,4 to 2,2 thats the problem
//这应该对某些灭绝起作用,唯一的问题是你不能从 4,4 到 2,2 这样的地方,这就是问题
回答by Reaganrewop
I have a little different method. Not sure whether this is better than ncurses package, so i leave that for the upvoters to decide.
我有一点不同的方法。不确定这是否比 ncurses 包更好,所以我把它留给支持者来决定。
You can use Graphicspackage in C++ to output a text to a specific coordinate on your working screen.
The syntax is outtextxy(x, y, text) ;
Where x & y are coordinates.
您可以使用C++ 中的Graphics包将文本输出到工作屏幕上的特定坐标。语法是outtextxy(x, y, text) ;
其中 x & y 是坐标。
One example is:
一个例子是:
int main(void) {
int gdriver = DETECT, gmode;
?? int x = 200, y = 200;
?
?? initgraph(&gdriver, &gmode, "c:\tc\bgi");
?
?? outtextxy(x, y, "Hello World");
?? closegraph();
}
This little program will print Hello Worldin the coordinate (200,200).
这个小程序将在坐标 (200,200) 中打印Hello World。
For reference to what graphics package can do visit this link
有关图形包可以做什么的参考,请访问此链接