如何在C ++中以特定坐标将字符串打印到控制台? [英] How can I print a string to the console at specific coordinates in C++?

查看:164
本文介绍了如何在C ++中以特定坐标将字符串打印到控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在控制台中以指定的坐标打印字符。到目前为止,我一直在使用非常难看的 printf( \033 [%d;%dH%s\n,2,2, str); 但是我只需要问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"); 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:

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

即使我强制转换为(char *)。
另一个问题是我必须打印出 \n 才能刷新页面...我只是不喜欢使用 printf

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.

简单地使用 cout 代替 printf ,我相信应该有一种更新的方式(理想情况下,这种方式可以让我轻松地在屏幕上的任意位置编写字符串,理想情况下,不需要需要以下奇怪的符号: \033 [%d;%dH

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)

所以,你们有我在寻找什么?

So, do any of you have what I'm looking for?

推荐答案

您正在做的是在纯C ++应用程序中使用一些非常终端特定的魔术字符。在此方法有效的同时,使用一个库可能会使您的工作轻松得多,该库使您不必处理终端特定的实现细节,并提供满足您需要的功能。

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.

调查curses库或ncurses库是否可用于您的系统。

Investigate whether curses or ncurses libraries are available for your system.

这篇关于如何在C ++中以特定坐标将字符串打印到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆