简体   繁体   English

如何在C ++中以特定坐标将字符串打印到控制台?

[英]How can I print a string to the console at specific coordinates in C++?

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"); 到目前为止,我一直在使用非常难看的printf("\\033[%d;%dH%s\\n", 2, 2, "str"); But I just had to ask whether C++ had any other way of doing this. 但是我只需要问C ++是否还有其他方法可以做到这一点。 The problem is not even that it's ugly, the problem comes up when I try to make myself a prettier function like so: 问题不只是丑陋,当我尝试使自己成为一个更漂亮的函数时,问题就出现了:

void printToCoordinates(int x, int y, string text)
{
    printf("\033[%d;%dH%s\n", x, x, text);
}

It doesn't work, even if I typecast to (char*) . 即使我强制转换为(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. 另一个问题是,我必须打印出\\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? 那么,你们中有人有我想要的吗?

诅咒是您想要的。

I remember using gotoxy(x,y) in Turbo C++ (conio.h) - don't know if it'll work for you though. 我记得在Turbo C ++(conio.h)中使用了gotoxy(x,y) -不知道它是否对您有用。 It moves the cursor to the coordinates specified by x and y . 它将光标移动到xy指定的坐标。

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);
}

A few improvements to your function: 对功能的一些改进:

void printToCoordinates(int x, int y, const char *format, ...)
{
    va_list args;
    va_start(args, format);
    printf("\033[%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 自动刷新stdout而不打印换行符
  • uses x and y in the format string (your use of x and x may have been a typo) 在格式字符串中使用xy (您对xx可能是拼写错误)

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),如其他答案所述。

What you are doing is using some very terminal specific magic characters in an otherwise pure C++ application. 您正在做的是在原本纯净的C ++应用程序中使用一些终端特有的魔术字符。 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. 在此方法有效的同时,您可能会更轻松地使用一个库,该库使您不必处理终端特定的实现细节,并提供满足您需要的功能。

Investigate whether curses or ncurses libraries are available for your system. 调查curses库或ncurses库是否可用于您的系统。

First: 第一:

void printToCoordinates(int x, int y, string text)
{
    printf("\033[%d;%dH%s\n", x, x, text);
}

You don't want to copy the string argument, you want to pass it by ( const ) reference. 您不想复制字符串参数,而是想通过( const )引用传递它。 Also, the (only) right way to get a char* from a std::string is to call its c_str() member function: 同样,从std::string获取char*的(唯一)正确方法是调用其c_str()成员函数:

void printToCoordinates(int x, int y, const std::string& text)
{
    printf("\033[%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. 关于您的问题:C ++无法完成您想要的事情,但是它允许您使用特定于平台的方法来做到这一点。 You would have to tell us your platform in order to get meaningful answers. 您必须告诉我们您的平台,以获得有意义的答案。

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的某个地方,这就是问题所在

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 Graphics package in C++ to output a text to a specific coordinate on your working screen. 您可以使用C ++中的Graphics包在工作屏幕上将文本输出到特定坐标。 The syntax is outtextxy(x, y, text) ; 语法为outtextxy(x, y, text) ; Where x & y are coordinates. 其中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 World in the coordinate (200,200). 这个小程序将在坐标(200,200)中打印Hello World

For reference to what graphics package can do visit this link 有关图形包可以做什么的参考,请访问此链接

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

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