如何使用 C++ 在 Windows 和 Linux 中清除控制台 [英] How do I clear the console in BOTH Windows and Linux using C++

查看:29
本文介绍了如何使用 C++ 在 Windows 和 Linux 中清除控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个跨平台的解决方案来清除用 C++ 编写的 Linux 和 Windows 中的控制台.这样做有什么功能吗?另请注意,我不希望最终用户程序员必须更改我程序中的任何代码以使其适用于 Windows 和 Linux(例如,如果它必须在两个函数之间进行选择,则必须做出决定在运行时或在编译时自主).

I need a cross platform solution for clearing the console in both Linux and Windows written in C++. Are there any functions in doing this? Also make note that I don't want the end-user programmer to have to change any code in my program to get it to clear for Windows vs Linux (for example if it has to pick between two functions then the decision has to be made at run-time or at compile-time autonomously).

推荐答案

简答:你不能.

更长的答案:使用 curses 库(Unix 上的 ncurses,Windows 上的 pdcurses).NCurses 应该可以通过您的包管理器获得,并且 ncurses 和 pdcurses 具有完全相同的界面(pdcurses 也可以独立于控制台创建窗口,其行为类似于控制台窗口).

Longer answer: Use a curses library (ncurses on Unix, pdcurses on Windows). NCurses should be available through your package manager, and both ncurses and pdcurses have the exact same interface (pdcurses can also create windows independently from the console that behave like console windows).

最难的答案:使用 #ifdef _WIN32 之类的东西让你的代码在不同的操作系统上表现不同.

Most difficult answer: Use #ifdef _WIN32 and stuff like that to make your code act differently on different operating systems.

这篇关于如何使用 C++ 在 Windows 和 Linux 中清除控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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