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

            人妻精品久久久久中文字幕一冢本 | 久久久久亚洲AV无码专区首JN| 香港aa三级久久三级| 久久激情亚洲精品无码?V| 狠狠综合久久AV一区二区三区| 久久综合给合久久狠狠狠97色69| 69国产成人综合久久精品| 品成人欧美大片久久国产欧美| 亚洲精品国产第一综合99久久| 久久综合88熟人妻| 久久影视综合亚洲| 久久精品午夜一区二区福利| 99久久国产主播综合精品| 久久SE精品一区二区| 久久996热精品xxxx| 蜜臀av性久久久久蜜臀aⅴ麻豆| 国产精品99久久久久久宅男| 狠狠精品久久久无码中文字幕| 99久久国产综合精品成人影院| 亚洲综合精品香蕉久久网| 久久久免费观成人影院| 国产精品对白刺激久久久| 久久综合亚洲色一区二区三区| 人人狠狠综合久久亚洲88| 久久久久99精品成人片试看| 久久99精品久久久久久齐齐| 91精品国产色综合久久| 狠狠精品久久久无码中文字幕| 看全色黄大色大片免费久久久| 2020久久精品国产免费| 色欲综合久久躁天天躁蜜桃| 久久只有这里有精品4| 看全色黄大色大片免费久久久| 精品久久久久久无码中文字幕| 久久久91精品国产一区二区三区 | 久久亚洲高清观看| 精品少妇人妻av无码久久| 久久狠狠爱亚洲综合影院| 国产99久久久国产精品小说| 7777精品伊人久久久大香线蕉| 久久国产亚洲精品|