getch와 getchar 질문 드립니다
목화
질문 제목 : getch와 getchar 질문 드립니다질문 내용 : 리눅스에서는 conio.h헤더파일이 없기 때문에 getch를 쓰지 못합니다. 그래서 getchar를 써야 됩니다
getch는 문자를 입력받을때 터미널창에 반환하지 않고 getchar는 반환을 합니다.
저는 리눅스에서 getchar를 써야됩니다. 그런데 화면에 입력값을 반환하고 싶지 않은데 어떻게 해야 될까요#include stdio.h
#include stdlib.h
#include termios.h
#include unistd.h
#include fcntl.h
static struct termios initial_settings, new_settings;
static int peek_character = -1;void delay(int n)
{
volatile int i,j;
for(i=0;in;i++);
{
for(j=0;j600;j++);
}
}
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(stdin_fileno, &oldt);
newt = oldt;
newt.c_lflag &= ~(icanon | echo);
tcsetattr(stdin_fileno, tcsanow, &newt);
oldf = fcntl(stdin_fileno, f_getfl, 0);
fcntl(stdin_fileno, f_setfl, oldf | o_nonblock);
ch = getchar();
tcsetattr(stdin_fileno, tcsanow, &oldt);
fcntl(stdin_fileno, f_setfl, oldf);
if(eof != ch)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
int getch(void)
{
int ch;
struct termios buf;
struct termios save; tcgetattr(0, &save);
buf = save;
buf.c_lflag &= ~(icanon|echo);
buf.c_cc[vmin] = 1;
buf.c_cc[vtime] = 0;
tcsetattr(0, tcsaflush, &buf);
ch = getchar();
tcsetattr(0, tcsaflush, &save);
return ch;
}
void main()
{
int a;
while(1){
while(1){
if(kbhit()){
a=getch();
switch(a){
case 49 : printf(1번키 누름:%d\n,a); break;
case 224 :{
a=getch();
printf(↑키 누름:%d\n,a); break;
}
}
}
else{
puts(wait);
delay(100000000);
}
// fflush(stdin);
}
}
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2698882 | 프로그래밍좀 짜주세요 (3) | 황예 | 2025-06-21 |
2698855 | 카프-라빈 알고리즘 코딩 분석좀 도와주세요.. | 꽃봄 | 2025-06-20 |
2698829 | 학점계산기 (7) | MyWay | 2025-06-20 |
2698782 | 기초적인 함수 질문이요ㅠㅠㅠㅠ | 내담 | 2025-06-20 |
2698749 | 프로그램 짜던 도중 패닉입니다...ㅜ | 파랑 | 2025-06-19 |
2698719 | 조건부컴파일 질문입니다.~ (2) | 큐트 | 2025-06-19 |
2698693 | 재귀 함수 에러 | 바닐라 | 2025-06-19 |
2698673 | 고민이있는데 들어좀주세요!! (1) | 초코맛캔디 | 2025-06-19 |
2698644 | 1부터 n까지의 합을 구하는데 엄청긴숫자의 합을 구할때는 어떻게 해야하나요? (4) | 슬우 | 2025-06-18 |
2698616 | 다른 함수로 안넘어갑니다..;;; | 도1도캣 | 2025-06-18 |
2698587 | 배열하다 막혀서... (3) | WhiteCat | 2025-06-18 |
2698559 | 문자열을 비우는방법 (2) | 하늘 | 2025-06-18 |
2698528 | 착하고 친절한 선생씌구해염~ㅋㅋ (4) | 옆집언니야 | 2025-06-17 |
2698502 | 자료구조 큐 | 캔서 | 2025-06-17 |
2698477 | 실행화면 배경문의요 | 선아 | 2025-06-17 |
2698430 | 변수의 값이 저장이 않되네요;; (4) | 피네 | 2025-06-16 |
2698404 | C#을 배울려고 하는데 C나 C++을 알아야 하나요 ?? (1) | 신당 | 2025-06-16 |
2698342 | 프로그램 질문점녀 (4) | 데빌의눈물 | 2025-06-16 |
2698318 | 파일 입출력 질문입니다~ (2) | 꽃 | 2025-06-15 |
2698291 | 문자 출력 함수 : putchar, fputc에 관하여. | 으뜸 | 2025-06-15 |