• <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)  編輯 收藏 引用

            久久综合色区| 免费精品国产日韩热久久| 久久精品中文闷骚内射| 久久精品草草草| 色8激情欧美成人久久综合电| 狠狠色婷婷久久综合频道日韩| 精品久久一区二区三区| 2021最新久久久视精品爱| 国内精品久久人妻互换| 蜜桃麻豆www久久国产精品| 久久免费看黄a级毛片| 成人精品一区二区久久久| 亚洲va久久久噜噜噜久久| 久久电影网| 亚洲一本综合久久| 久久大香香蕉国产| 伊人久久大香线蕉亚洲| 久久午夜无码鲁丝片午夜精品| 国内精品久久久久久99蜜桃| 一97日本道伊人久久综合影院| 91精品婷婷国产综合久久| 国产精品久久久久jk制服| 久久丫忘忧草产品| 久久天天婷婷五月俺也去| 欧美午夜精品久久久久久浪潮| 26uuu久久五月天| 久久夜色tv网站| 亚洲一区中文字幕久久| 国产欧美一区二区久久| 久久婷婷国产麻豆91天堂| 国产精品岛国久久久久| 色综合久久天天综合| 色综合合久久天天综合绕视看 | 精品乱码久久久久久夜夜嗨| 国产精品毛片久久久久久久 | 精品久久久久中文字幕一区| 青青草国产成人久久91网| 久久这里只精品国产99热| 日本精品久久久中文字幕| 久久国产成人| 亚洲色大成网站www久久九|