gotoxy()
函数gotoxy()
函数用于将光标移动到屏幕上的指定位置。在 DOS 环境中,通常使用 conio.h
头文件中的 gotoxy()
函数。以下是一个简单的示例:
#include <iostream>
#include <conio.h> // 包含 conio.h 头文件
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main() {
gotoxy(10, 5); // 将光标移动到 (10, 5) 位置
std::cout << "Hello, World!";
return 0;
}
clrscr()
函数clrscr()
函数用于清除屏幕上的内容。在 DOS 环境中,通常使用 conio.h
头文件中的 clrscr()
函数。以下是一个简单的示例:
#include <iostream>
#include <conio.h> // 包含 conio.h 头文件
int main() {
std::cout << "This is a test message.\n";
clrscr(); // 清除屏幕
std::cout << "Screen cleared.\n";
return 0;
}
在现代的 C++ 环境中,conio.h
头文件可能不可用。可以使用其他方法实现类似的功能:
gotoxy()
函数可以使用 ANSI 转义序列来移动光标。以下是一个示例:
#include <iostream>
void gotoxy(int x, int y) {
std::cout << "\033[" << y << ";" << x << "H";
}
int main() {
gotoxy(10, 5); // 将光标移动到 (10, 5) 位置
std::cout << "Hello, World!";
return 0;
}
clrscr()
函数可以使用 ANSI 转义序列来清除屏幕。以下是一个示例:
#include <iostream>
void clrscr() {
std::cout << "\033[2J\033[1;1H"; // 清除屏幕并将光标移动到 (1, 1) 位置
}
int main() {
std::cout << "This is a test message.\n";
clrscr(); // 清除屏幕
std::cout << "Screen cleared.\n";
return 0;
}
gotoxy()
和 clrscr()
函数在 DOS 环境中非常有用。原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。