Linux 下沒有conio.h 已解決


原文:http://blog.sina.com.cn/s/blog_6a95e00b0100zqvf.html

 

#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是不顯示輸入內容的意思


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM