Pause功能的实现

来自KlniuWiki
跳转到: 导航, 搜索
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
 
int getch();
void press_key();
 
int main()
{
    printf("Hello world!\n");
    press_key();
    return 0;
}
 
void press_key()
{
    printf("Press any key to continue...\n");
    getch();
}
 
int getch()
{
    struct termios tm,tm_old;
    int fd = STDIN_FILENO,c;
 
    if (tcgetattr(fd, &tm) < 0)
    {
        return -1;
    }
 
    tm_old = tm;
    cfmakeraw(&tm);
 
    if (tcsetattr(fd,TCSANOW, &tm) < 0)
    {
        return -1;
    }
 
    c = fgetc(stdin);
 
    if (tcsetattr(fd,TCSANOW,&tm_old) < 0) 
    {
        return -1;
    }
    return c;
}

参考文献

个人工具
分类
化学
[×] 國學
学佛
[×] 数学
物理
生活
[×] 英语
读书
辞典
廣告