C++ 如何替换命令提示符上已有的文本?C++;
我想知道是否可以将键盘输入后的文本“[1]example1[2]example2”替换为屏幕上已有的“example1extended”。 类似于系统(“CLS”);但只有一行文字C++ 如何替换命令提示符上已有的文本?C++;,c++,C++,我想知道是否可以将键盘输入后的文本“[1]example1[2]example2”替换为屏幕上已有的“example1extended”。 类似于系统(“CLS”);但只有一行文字 int main() //just an example { int ans; std::cout << "[1] example1 [2] example2" << std::endl; std::cout << "enter a choice: ";
int main() //just an example
{
int ans;
std::cout << "[1] example1 [2] example2" << std::endl;
std::cout << "enter a choice: ";
std::cin >> ans;
if (ans == 1)
{
std::cout << "example1extended" << std::endl;
}
}
int main()//只是一个例子
{
INTANS;
STD:首先,C++没有“屏幕”,只有“输入”和“输出”是“典型”的终端,但这个终端的行为不是C++标准的一部分,因为它不是可移植的,所以不同终端,特别是在不同OS上的结果是不同的。
如果您使用的终端支持VT100,则可以使用特殊字符控制光标并清除终端屏幕上的字符
<0 >有数百个图书馆正在处理这样的终端(仿真器)。 首先,C++没有“屏幕”,只有输入和输出到“某物”。这是一个典型的终端,但是这个终端的行为不是C++标准的一部分,因为它不是可移植的,所以不同终端的结果,特别是在不同的操作系统上的结果是不同的。
如果您使用的终端支持VT100,则可以使用特殊字符控制光标并清除终端屏幕上的字符
周围有数百个库正在处理此类终端(模拟器)。没有独立于平台的方法,但由于您提到了系统(“cls”)
我假设您是在Windows上
Windows具有,这是一组用于操作控制台的实用程序函数。此处有2个选项:
设置光标的位置并用空格覆盖:
#include <windows.h>
..
auto consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordsToDelete = { row, col }; // the coordinates of what you want to delete
// Move the current cursor position back. Writing with std::cout will
// now print on those coordinates
::SetConsoleCursorPosition(consoleHandle, position);
// This will replace the character at (row, col) with space.
// Repeat as many times as you need to clear the line
std::cout << " ";
#包括
..
自动控制台句柄=GetStdHandle(标准输出句柄);
COORD coordsToDelete={row,col};//要删除内容的坐标
//向后移动当前光标位置。使用std::cout写入将
//现在打印这些坐标
::设置控制台或位置(控制台手柄,位置);
//这将用空格替换(行、列)处的字符。
//根据需要重复多次以清除线路
std::cout没有独立于平台的方法,但是既然您提到了system('cls')
,我假设您在Windows上
Windows具有,这是一组用于操作控制台的实用程序函数。此处有2个选项:
设置光标的位置并用空格覆盖:
#include <windows.h>
..
auto consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordsToDelete = { row, col }; // the coordinates of what you want to delete
// Move the current cursor position back. Writing with std::cout will
// now print on those coordinates
::SetConsoleCursorPosition(consoleHandle, position);
// This will replace the character at (row, col) with space.
// Repeat as many times as you need to clear the line
std::cout << " ";
#包括
..
自动控制台句柄=GetStdHandle(标准输出句柄);
COORD coordsToDelete={row,col};//要删除内容的坐标
//向后移动当前光标位置。使用std::cout写入将
//现在打印这些坐标
::设置控制台或位置(控制台手柄,位置);
//这将用空格替换(行、列)处的字符。
//根据需要重复多次以清除线路
std::cout除非您直接修改帧缓冲区,或找到一个用于此目的的包或实用程序,否则不会。除非您直接修改帧缓冲区,或找到一个用于此目的的包或实用程序,否则可能会重复查找curses库。不会。可能会重复查找curses库