conio.h 在 Linux 中如何使用

小樊
82
2024-09-21 23:15:49

conio.h 是一個(gè)在 Windows 編程中常用的頭文件,它提供了一些用于控制控制臺(tái)輸出的函數(shù),如 kbhit()getch()。然而,這個(gè)頭文件在 Linux 系統(tǒng)中并不存在。

如果你想在 Linux 系統(tǒng)中進(jìn)行類似的控制臺(tái)輸入輸出操作,你可以使用標(biāo)準(zhǔn)輸入輸出流(stdinstdout)以及 C++11 引入的 <iostream> 頭文件。下面是一些基本的示例:

  • 讀取單個(gè)字符:
#include <iostream>
#include <unistd.h> // 提供 sleep 函數(shù)
#include <termios.h> // 提供 termios 相關(guān)函數(shù)

int main() {
    std::cout << "Press any key to continue...";
    std::cin.get(); // 等待用戶輸入
    std::cout << "You pressed " << std::cin.get() << '\n';
    sleep(1); // 等待 1 秒
    return 0;
}
  • 讀取一行文本:
#include <iostream>
#include <string>

int main() {
    std::cout << "Enter a line of text: ";
    std::string input;
    std::getline(std::cin, input);
    std::cout << "You entered: " << input << '\n';
    return 0;
}

這些示例展示了如何在 Linux 系統(tǒng)中使用 C++ 進(jìn)行基本的控制臺(tái)輸入輸出操作。

0