[問題] linux C 如何不按enter輸入字元?

看板C_and_CPP (C/C++)作者 (從數學發現心世界)時間14年前 (2011/09/12 23:17), 編輯推噓2(2010)
留言12則, 7人參與, 最新討論串1/2 (看更多)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) linux 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 做出 getch() 不需要輸入enter的動作 程式碼(Code):(請善用置底文網頁, 記得排版) #include <stdio.h> int readch () { unsigned char c; read (fileno(stderr), &c, 1); return (c); } int main(void) { char c; puts("請輸入..Y/N "); while(1) { c=readch(); if(c=='y') return 0; if(c=='n') return 0; } return 0; } 補充說明(Supplement): 一開始找到curses.h這個有getch(),不過看起來不是我以前認識的getch() 後來參考了more原碼,不過more的作法好像是先把緩衝塞滿然後讓它溢位, 抓最後一個值回傳來達到不按enter的動作..有沒有更簡單的作法? 能像getch()函式一樣不用按enter呢? 還有puts如果改成printf執行起來不會先跑出 "請輸入..Y/N "這行? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.255.19.203

09/13 00:21, , 1F
termios.h
09/13 00:21, 1F

09/13 00:32, , 2F
read應該在unistd吧...
09/13 00:32, 2F

09/13 00:34, , 3F
而且你從stderr讀字元....
09/13 00:34, 3F

09/13 02:05, , 4F
Google一下,之前看過有人做過一樣的
09/13 02:05, 4F

09/13 02:35, , 5F
09/13 02:35, 5F

09/13 06:21, , 6F
對,就是樓上的東西
09/13 06:21, 6F

09/13 09:16, , 7F
tropical72守備範圍廣到,讓我覺得犯規了<O.O>
09/13 09:16, 7F

09/13 10:05, , 8F
超廣的
09/13 10:05, 8F

09/13 12:29, , 9F
癢 我眼殘了
09/13 12:29, 9F

09/13 12:44, , 10F
...樓上走錯間了
09/13 12:44, 10F

09/13 13:38, , 11F
terminal raw mode (linux一般是用cooked)
09/13 13:38, 11F

09/13 13:40, , 12F
(line buffered)
09/13 13:40, 12F
文章代碼(AID): #1ERYAHAS (C_and_CPP)
文章代碼(AID): #1ERYAHAS (C_and_CPP)