• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            大龍的博客

            常用鏈接

            統計

            最新評論

            不等待回車,讀取控制臺輸入-------------------------轉

            在很多情況下,我們希望在控制臺下,按鍵盤字符,程序馬上反應而不是等待回車后才響應。

                在Windows平臺下可以使用getch  ()(要求#include “conio.h“)實現,而在Linux平臺下沒有這個頭文件,也就無法使用這個函數。
                車到山前必有路,我們另有辦法。
                先看下面這段代碼:
            struct termios stored_settings;
            struct termios new_settings;
            tcgetattr (0, &stored_settings);
            new_settings = stored_settings;
            new_settings.c_lflag &= (~ICANON);
            new_settings.c_cc[VTIME] = 0;
            new_settings.c_cc[VMIN] = 1;
            tcsetattr (0, TCSANOW, &new_settings);
                termios結構的描述了終端的模式,在這段代碼中我們改變了它,使得終端能夠接收到鍵盤輸入馬上返回。所以就能夠使用一般的讀取字符函數getchar ()來獲得輸入字符。
                在退出你的程序時,要記得把終端環境改回來:
            tcsetattr (0, TCSANOW, &stored_settings);

            這幾個函數以及結構要求包含頭文件termios.h和stdio.h。
                下面是一個測試文件,可以在Windows和Linux操作系統下,編譯運行:

            #include "stdio.h"
            #include "stdlib.h"
            #ifdef _WIN32 //Linux platform
                #include "conio.h"
                #define get_char getch
            #else
                #include "termios.h"
                #define get_char getchar
            #endif

            int main (int argc, char* argv[])
            {
                #ifdef _WIN32
                //Do nothing
                #else
                struct termios stored_settings;
                struct termios new_settings;
                tcgetattr (0, &stored_settings);
                new_settings = stored_settings;
                new_settings.c_lflag &= (~ICANON);
                new_settings.c_cc[VTIME] = 0;
                new_settings.c_cc[VMIN] = 1;
                tcsetattr (0, TCSANOW, &new_settings);
                #endif
                while (1) {
                    char c = get_char ();
                    if ('q' == c || 'Q' == c)
                        break;
                    printf ("You input: %c\n", c);
                }
                #ifdef _WIN32
                //Do nothing
                #else
                tcsetattr (0, TCSANOW, &stored_settings);
                #endif
                return 0;
            }
                要提的一點是,getch ()是沒有回顯的,而getchar ()是有回顯的,所以在Windows和Linux下的運行有點不同。

            posted on 2007-07-16 12:47 大龍 閱讀(2180) 評論(0)  編輯 收藏 引用

            亚洲中文字幕无码久久2020| 99久久国产亚洲综合精品| 无码人妻久久一区二区三区免费丨| 一本一道久久a久久精品综合| 久久久久99这里有精品10 | 国产午夜精品理论片久久 | 亚洲精品乱码久久久久久不卡| 久久人人爽人人精品视频| 久久99精品国产麻豆宅宅| 丁香五月网久久综合| 久久香蕉综合色一综合色88| 久久国产免费| 亚洲乱码中文字幕久久孕妇黑人| 国产精品久久久久天天影视| 久久综合亚洲色HEZYO国产| 无码精品久久久天天影视| 久久久精品国产亚洲成人满18免费网站 | 精品久久久久久中文字幕人妻最新| 久久精品男人影院| 久久精品国产亚洲αv忘忧草| 久久精品aⅴ无码中文字字幕不卡| 国产毛片久久久久久国产毛片| 久久久久亚洲av成人网人人软件| 日本道色综合久久影院| 久久精品中文闷骚内射| 久久久久免费精品国产| 久久久久国产视频电影| 情人伊人久久综合亚洲| 久久精品国产亚洲AV麻豆网站| 午夜精品久久久久久影视777| 91精品国产91久久| 精品久久久久久成人AV| 日韩人妻无码一区二区三区久久 | 亚洲AV无码久久精品狠狠爱浪潮 | 武侠古典久久婷婷狼人伊人| 久久精品欧美日韩精品| 久久免费看黄a级毛片| 久久一本综合| 亚洲精品tv久久久久| 欧美久久综合九色综合| 青春久久|