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);
}
}
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2693676 | 열혈강의 c언어 질문입니다 | 하양이 | 2025-05-04 |
2693647 | 12.620000 을요 12.620 으로 어떻게 표현해요? (2) | 파도 | 2025-05-04 |
2693619 | 타이틀 코드.. | 단순드립 | 2025-05-03 |
2693591 | 컴파일 에러에서 질문드립니다 (3) | 게자리 | 2025-05-03 |
2693463 | 동적할당 이용시 fwrite사용을 어떻게 해야하나요..? (10) | 일본어못해요 | 2025-05-02 |
2693387 | 배열문제입니다 수정오류캡쳐했습니다 (6) | 연하얀 | 2025-05-01 |
2693356 | text 입출력 내림차순 질문입니다 ㅠ | 빛글 | 2025-05-01 |
2693328 | C언어를이용해서 .txt파일 외에 다른 확장자 파일 삭제가 가능한지.. (2) | 대나무 | 2025-05-01 |
2693299 | 파일입출력 바이너리파일 | 독특한 | 2025-04-30 |
2693273 | 오류 (1) | 귀1여운렩 | 2025-04-30 |
2693080 | visual studio 2008 express edition 등록키 말인데요 | 얀별 | 2025-04-28 |
2693053 | 배열, 구조체 관련 프로그래밍 질문드립니다. | 싸리 | 2025-04-28 |
2693025 | 프로그램을 짜봤는데요 ㅠㅠ | 상처입은마음 | 2025-04-28 |
2693001 | 워닝문제, 세그멘트결함문제 (1) | 월식 | 2025-04-28 |
2692979 | 라인한줄 이랑.. 소스 설명좀 부탁드려요.. | 이루리 | 2025-04-27 |
2692947 | 이 문제좀 풀어 주세요..ㅜㅜ (1) | 소리 | 2025-04-27 |
2692889 | 함수의 구조체 인자로 받아서 그 인자로 데이터 넣기... | 한뎃집 | 2025-04-27 |
2692862 | 성적 출력 하는 프로그램인데요~!!!도움좀 주세욤.ㅠ | 두빛나래 | 2025-04-26 |
2692831 | if 문 간단해요 빨리좀 ㅠㅠ | 이플 | 2025-04-26 |
2692805 | 실행파일이 이상해요 | 푸헷 | 2025-04-26 |