• <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 大龍 閱讀(2170) 評論(0)  編輯 收藏 引用

            WWW婷婷AV久久久影片| 美女久久久久久| 日产精品久久久久久久| 久久综合亚洲欧美成人| 久久99国产乱子伦精品免费| 蜜桃麻豆www久久| 久久精品国产欧美日韩99热| 人妻精品久久久久中文字幕69 | 国产精品久久一区二区三区 | 青青草原综合久久大伊人| 少妇久久久久久被弄高潮| 国产精品成人久久久久三级午夜电影| 久久伊人中文无码| 久久99精品国产自在现线小黄鸭| 精品久久国产一区二区三区香蕉| 偷偷做久久久久网站| 久久久久国产视频电影| 国产精品一区二区久久不卡 | 国产99精品久久| 精品国产日韩久久亚洲| 亚洲精品高清久久| 精品久久无码中文字幕| 久久久久久久久波多野高潮| 久久国产精品免费一区二区三区| 色妞色综合久久夜夜| 亚洲精品午夜国产va久久| 久久精品国产99久久丝袜| 久久青草国产精品一区| 99久久免费国产特黄| 久久精品国产亚洲av影院| 亚洲va中文字幕无码久久不卡| 亚洲国产成人久久综合一区77| 秋霞久久国产精品电影院| 国内精品久久人妻互换| 色综合久久久久久久久五月| 久久精品无码一区二区WWW| 久久ZYZ资源站无码中文动漫 | 伊色综合久久之综合久久| 久久亚洲AV永久无码精品| 香蕉久久AⅤ一区二区三区| 开心久久婷婷综合中文字幕|