#include <stdio.h>
//#include <conio.h>
void main(){
char ch;
for(;;){
// system("stty -echo");
ch = getch();
if(ch==27) break;
if(ch==13)
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>
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是不顯示輸入內容的意思
參考博客:https://blog.csdn.net/qq_36955347/article/details/72794934