【问题标题】:Substitute for getch(), gotoxy(), delay(), clrscr()替代 getch()、gotoxy()、delay()、clrscr()
【发布时间】:2011-04-17 11:06:47
【问题描述】:

我有一些为 Turbo C 编译器编写的旧程序源代码。我对它们进行了更改,并希望在 Linux 和 Windows 的较新编译器中重新编译它们。所以请告诉我什么是最好的替代功能

getch(), delay() / sleep(), clrscr(), 高氧()

用于 C 和 C++。

【问题讨论】:

    标签: c++ c


    【解决方案1】:

    查看适用于 Unix 兼容系统的 ncurses 库。

    【讨论】:

      【解决方案2】:

      对于 Windows 系统:

      最好将程序编译为 Windows 的控制台应用程序。

      您可以直接将 Windows API 用于控制台窗口和控制台输出。 看看MSDN:Windows Console functions

      以下是给定函数的可能替换项:

      • getch():使用 conio.h 中的 _getch()
      • delay()/sleep():使用windows的Sleep()函数
      • clrscr():使用 FillConsoleOutputCharacter() 和 FillConsoleOutputAttribute() 编写自己的 clrscr() 函数
      • gotoxy():使用 SetConsoleCursorPosition()

      【讨论】:

      • system("cls") 应该为clrscr 做这项工作。 :)
      【解决方案3】:

      在类 Unix 系统上,您可以使用 VT100 转义码替换 clrscr() 和 gotoxy()。 clrscr():

      std::cout << "\033[2J" << std::flush;
      

      请参阅 http://www.termsys.demon.co.uk/vtansi.htm 了解 gotoxy() 等。

      【讨论】:

        【解决方案4】:
        1. 要替换clrscr(),您可以使用system("cls");(可用于#include&lt;stdlib.h&gt;)。
        2. 要替换getch(),您可以使用system("pause");(也可以使用#include&lt;stdlib.h&gt;)。

        【讨论】:

        • 您好,欢迎来到 SO !请同时添加 gotoxy() 和 delay() 的替代候选者。
        • system("pause")getch非常有限替代。并且 OP 也希望与 Linux 兼容,而且两者都是仅适用于 Windows 的......
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多