한문자만 입력받는 함수 없나요?

익명 사용자의 이미지

제목 그대로 한 문자만 입력받아서 처리하는 함수를 찾고있는데 없어서
이렇게 글 올립니다.

제가 하려는건 텍스트 메뉴가 있고 이 중에서 해당하는 번호를 선택하는
데 번호선택시 입력되는 문자를 2문자로 제한하고 싶거든요.
만약 "02"라고 입력하면 더이상 입력되지 않게 하려구요...
그리고 Back-Space (<-)를 입력하면 "^H"라고 찍히는데 이를 문자가 찍히
지 않고 이미 입력된 마지막 문자를 삭제하는 원래의 기능 그대로 구현하
기 위해서 입력받는 문자열 중 한 문자당 바로 처리할 수 있는 함수를 구
하거나 작성할 수 있는 조언을 얻고자 합니다.

getc(), getchar(), fgets()...등은 모두 엔터키가 들어올 때 까지의 문
자열을 처리 하더군요.

키보드를 입력할 때 마다 처리할 수 있는 방법을 가르쳐 주시면 감사하겠
습니다.

즐거운 하루 되세요.

익명 사용자의 이미지

fputc를 찾아 보세요

백스페이스 구현은 여기서 제 이름이나 '백스페이스'로 검색하심 나올겁니
다.

그럼 즐프하세요

익명 사용자의 이미지

ncurses 또는 curses 라이브러리 중에
윈도우(DOS)의 Turbo C처럼 int getch()함수가 있습니다만
를 포함해야 하고, 컴파일할때 -lncurses 또는 -lcurses라고 해
줘야합니다.

익명 사용자의 이미지

이름 입력해도...말씀하신 "백스페이스"입력해도 나오질 않습니다.

제가 잘못 친건가???

혹시 처리할 수 있는 간단한 방법이 있으면 조언 부탁드립니다.

현재 하고있는 부분이 콘솔 제어해서 문자 하나씩 처리를 하려고 하는데
특수키를 제어하는데 그냥 쉽게 되질 않네요.
혹시 이를 처리할 수 있는 방법을 알고 계시면 조언 부탁드립니다.

수고하세요.

익명 사용자의 이미지

그건 저수준 터미널 인터페이스를 이용하시는것이 좋을듯 하네요.

위에 님이 말씀하신대로 getch()같은 함수도 터미널함수

tcgetattr()등으로 구현이 가능합니다.

문자 입력수 같은것도 제어가 가능한걸로 알고있습니다.

자세한건 gnu c reference를 참조하시길.

man termios나 야후에서 치면 그냥 나오는군요 -)

익명 사용자의 이미지

안녕하세요.

이 부분은 termios.h 부분을 보면은 어느 정도 답이 나오지 않을까하는 생
각이 드네요.
이 부분을 참고 해 보세요.

_ 信

익명 사용자의 이미지

제가 착각을 했었네요..

제가 이곳에 질문했던것은 백스페이스가 아니라 특수문자 였군요.. ㅡㅡ;

제가 쓰는 터미널에서 백스페이스가 ^H 였나? 이런걸로 나오기 때문에

저도 백스페이스를 강제로 만든적이 있습니다.

사용은

char password[20];

strcpy(password, (char *)stdin_func(0));

의 식입니다. stdin_func()의 인자가 0이면 콘솔에 보이는 문자는 무조건 * 이 되고,
여타 다른 숫자면 그냥 찍히는데로 보입니다.

별로 좋은 소스는 아니지만(초보란 말입니다. ㅡㅡ;) 도움을 될듯 해서 올려 드립니다.

#include
#include
/********************************************
**********************************************/
char* stdin_func(int lev){
char inputchar;
char pass[20];
int i=0;
int flag=0;
int keynum = 0;

struct termios new_term, old_term;

tcgetattr(fileno(stdin), &old_term);
new_term = old_term;

new_term.c_lflag &= ~(ICANON | ECHO);

tcsetattr(fileno(stdin), TCSANOW, &new_term);

memset(pass, 0x00, sizeof(pass));

while(1){
if( ( inputchar = fgetc(stdin) ) == '\n' )
break;
if( inputchar == 8){
if(!keynum)
continue;
keynum--;
pass[i] = (char)NULL;
if(i)
i--;
pass[i] = (char)NULL;
fputc(8, stdout);
fputc(32, stdout);
fputc(8, stdout);
continue;
}
else{
keynum++;
if(lev == 0)
fputc('*', stdout);
else
fputc(inputchar, stdout);
pass[i] = inputchar;
}
i++;
if(i>20)
break;
}
tcsetattr(fileno(stdin), TCSANOW, &old_term);
strcpy(pass, pass);
return pass;
};

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.