原文:http://blog.sina.com.cn/s/blog_6a95e00b0100zqvf.html
linux下沒有conio.h的頭文件,想要實現getch就無法實現,但是有辦法可以代替
include <stdio.h>
//#include <conio.h>
void main(){
char ch;
for(;😉{
// system("stty -echo");
ch = getch();
if(ch27) break;
if(ch13)
continue;
putch(ch);
}
}
Linux實現conio.h中的getch()功能
在windows下寫C程序時有時會用到conio.h這個頭文件中的getch()功能,即讀取鍵盤字符但是不顯示出來(without echo)
后來發現含有conio.h的程序在linux無法編譯通過,因為linux沒有這個頭文件,今天突然發現可以用其他方法代替,貼出來
//in windows
include<stdio.h>
include<conio.h>
int mian(){
char c;
printf("input a char:");
c=getch();
printf("You have inputed:%c \n",c);
return 0;
}
//in linux
include<stdio.h>
include <stdlib.h> //原文沒有這句,但是會出現system的警告
int main(){
char c;
printf("Input a char:");
system("stty -echo");
c=getchar();
system("stty echo");
printf("You have inputed:%c \n",c);
return 0;
}
這樣就可以了,注:linux中stty -echo是不顯示輸入內容的意思