getchar()问题 [英] getchar() problem

查看:75
本文介绍了getchar()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在Linux Fedora Core 4下使用GCC编译的标准C


当我运行此程序并在提示符下输入字符时,我必须按ENTER键才能按

。这给了我2个输入字符 - ''a''和

''\ n''(Hex 61和0a)


好​​像好像getchar()函数需要ENTER才能终止读取

stdin。


当我只按一个键时,我试图让程序响应(即

,无需按ENTER键。)


程序

~~~~~~~

#include< stdio.h>

#include< stdlib.h>


int main()

{

char x; //输入字符

while(1)

{

printf(">"); //打印提示''>''

x = getchar(); //获取输入字符

printf("%d%02x \ n",x,x); //以十进制打印字符&十六进制

if(x ==''q'')//如果字符是q则退出

exit(0);

}

}


输出

~~~~~


I am using Standard C compiled with GCC under Linux Fedora Core 4

When I run this program and enter a character at the prompt, I have to press
the ENTER key as well. This gives me 2 input characters - ''a'' and
''\n'' (Hex 61 and 0a)

It seems as though the getchar() function needs ENTER to terminate reading
stdin.

I am trying to get the program to respond when I press one key only (ie
without needing to press ENTER as well).

Program
~~~~~~~
#include <stdio.h>
#include <stdlib.h>

int main()
{
char x; //Input character
while (1)
{
printf(">"); //Print prompt ''>''
x = getchar(); //Get input character
printf("%d %02x \n", x, x);//Print character in decimal & Hex
if (x == ''q'') //Exit if the character is ''q''
exit(0);
}
}

Output
~~~~~

a<输入字符
97 6110 0a
b<输入字符
98 6210 0a
c<输入字符
99 6310 0a
d<输入字符
100 6410 0a
q<输入字符
a < input character 97 6110 0a
b < input character 98 6210 0a
c < input character 99 6310 0a
d < input character 100 6410 0a
q < input character



113 71



关于我如何只能输入一个字符的任何想法? TIA :)


Alan


113 71

I have tried using scanf() but got the same results.

Any ideas as to how I can input ONE character only? TIA :)

Alan

推荐答案

Alan写道:
Alan wrote:

我在Linux Fedora Core 4下使用GCC编译的标准C

当我运行此程序并在提示符下输入一个字符时,我必须按
ENTER键也是如此。这给了我2个输入字符 - ''a''和
''\ n''(Hex 61和0a)

好像getchar()函数需要ENTER来终止阅读
stdin。

当我只按一个键时,我试图让程序响应(即
而不需要按ENTER键)。

程序
~~~~~~~
#include< stdio.h>
#include< stdlib.h>

int main ()
{x / x //输入字符
while(1)
{
printf(">"); //打印提示''>''
x = getchar(); //获取输入字符
printf("%d%02x \ n",x,x); //打印十进制字符&
Hex
if(x ==' 'q'')//退出,如果字符是''q''
退出(0);
}
}

输出
〜 ~~~~

I am using Standard C compiled with GCC under Linux Fedora Core 4

When I run this program and enter a character at the prompt, I have to
press
the ENTER key as well. This gives me 2 input characters - ''a'' and
''\n'' (Hex 61 and 0a)

It seems as though the getchar() function needs ENTER to terminate reading
stdin.

I am trying to get the program to respond when I press one key only (ie
without needing to press ENTER as well).

Program
~~~~~~~
#include <stdio.h>
#include <stdlib.h>

int main()
{
char x; //Input character
while (1)
{
printf(">"); //Print prompt ''>''
x = getchar(); //Get input character
printf("%d %02x \n", x, x);//Print character in decimal &
Hex
if (x == ''q'') //Exit if the character is ''q''
exit(0);
}
}

Output
~~~~~
a<输入字符


97 61


97 61

10 0a
b<输入字符
10 0a
b < input character


98 62


98 62

10 0a
c<输入字符
10 0a
c < input character


99 63


99 63

10 0a
d<输入字符
10 0a
d < input character


100 64


100 64

10 0a
q<输入字符
10 0a
q < input character


113 71

我尝试过使用scanf()但得到了相同的结果。

关于如何输入ONE的任何想法只有人物? TIA :)

Alan


113 71

I have tried using scanf() but got the same results.

Any ideas as to how I can input ONE character only? TIA :)

Alan




在网上搜索kbhit.c那里有一个版本的linux for i,br />
使用它很多,它解决了这些问题。只需将kbhit.c

文件添加到Makefile中,然后输入#include" kbhit.h"在你的程序中。

在程序顶部调用init_keyboard(),在

退出之前或之前调用close_keyboard()。这两个步骤非常重要,不要忘记它们。

警告:在你的程序退出之前未能调用close_keyboard()

让你的keybaord处于一个非常奇怪的状态。 br />

啊,他的足够短 - 这里......

Eric


/ *以下代码是kbhit .c * /

#include" kbhit.h"

#include< termios.h>

#include< unistd。 h取代; // for read()


static struct termios initial_settings,new_settings;

static int peek_character = -1;


void init_keyboard()

{

tcgetattr(0,& initial_settings);

new_settings = initial_settings;

new_settings.c_lflag& = ~ICANON;

new_settings.c_lflag& =〜ECHO;

new_settings.c_lflag& = ~ISIG;

new_settings.c_cc [VMIN] = 1;

new_settings.c_cc [VTIME] = 0;

tcsetattr(0,TCSANOW,& new_settings);

}


void close_keyboard()

{

tcsetattr(0,TCSANOW,& initial_settings) ;

}


int kbhit()

{

unsigned char ch;

int nread;


如果(peek_character!= -1)返回1;

new_settings.c_cc [VMIN] = 0;

tcsetattr(0,TCSANOW,& new_settings);

nread = read(0,& ch,1);

new_settings.c_cc [ VMIN] = 1; <无线电通信/>
tcsetattr(0,TCSANOW,& new_settings);

if(nread == 1)

{

peek_character = ch;

返回1;

}

返回0;

}

int readch()

{

char ch;


if(peek_character!= -1)

{

ch = peek_character;

peek_character = -1;

返回ch;

}

读取(0,& ch,1);

返回ch;

}

/ *

kbhit.c EOF

* /

/ *以下代码为kbhit.h * /

#ifndef __KBHIT_H__

#define __KBHIT_H__

void init_keyboard(void);

void close_keyboard(void);

int kbhit(void);

int readch(void);

#endif


/ *

kbhit.h EOF

* /


/ *

像这个快速而肮脏的例子一样使用它

* /

#include< stdlib.h>

#include< stdio .h>

#include" kbhit.h"

int main(无效)

{

int ch;


init_keyboard(); //每个程序运行一次


//使用它的一种方法

if(kbhit())ch = readch();


//这里是另一个样本用法

do {

if(kbhit())// kbhit立即返回,按下按键或不是
{

ch = readch();

//根据ch />
的值做任何事情if(ch =''q'')休息;

}

sleep(1);

} while(1);


close_keyboard();


返回0;

}



Search the net for kbhit.c there''s a version of it out there for linux, i
use it a lot and it solves these kinds of problems. Just add the kbhit.c
file to your Makefile and put #include "kbhit.h" in your program.
Call init_keyboard() at top of program and close_keyboard() on or before
exit. These two steps are really important, dont forget them.
Warning: failure to call close_keyboard() before your program exits will
leave your keybaord in a really weird state.

Ah hell, its short enough - Here...
Eric

/* The following code is kbhit.c */
#include "kbhit.h"
#include <termios.h>
#include <unistd.h> // for read()

static struct termios initial_settings, new_settings;
static int peek_character = -1;

void init_keyboard()
{
tcgetattr(0,&initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_lflag &= ~ISIG;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_settings);
}

void close_keyboard()
{
tcsetattr(0, TCSANOW, &initial_settings);
}

int kbhit()
{
unsigned char ch;
int nread;

if (peek_character != -1) return 1;
new_settings.c_cc[VMIN]=0;
tcsetattr(0, TCSANOW, &new_settings);
nread = read(0,&ch,1);
new_settings.c_cc[VMIN]=1;
tcsetattr(0, TCSANOW, &new_settings);
if(nread == 1)
{
peek_character = ch;
return 1;
}
return 0;
}

int readch()
{
char ch;

if(peek_character != -1)
{
ch = peek_character;
peek_character = -1;
return ch;
}
read(0,&ch,1);
return ch;
}
/*
kbhit.c EOF
*/
/* The following code is kbhit.h */
#ifndef __KBHIT_H__
#define __KBHIT_H__
void init_keyboard(void);
void close_keyboard(void);
int kbhit(void);
int readch(void);
#endif

/*
kbhit.h EOF
*/

/*
Use it like this quick and dirty example
*/
#include <stdlib.h>
#include <stdio.h>
#include "kbhit.h"
int main(void)
{
int ch;

init_keyboard(); // run this once per program

// one way to use it
if(kbhit()) ch = readch();

// here''s another sample usage
do {
if(kbhit()) // kbhit returns immediately, key pressed or not
{
ch = readch();
// do whatever here based on value of ch
if(ch=''q'') break;
}
sleep(1);
} while(1);

close_keyboard();

return 0;
}


Eric写道:
Alan写道:
Alan wrote:
我在Linux Fedora Core 4下使用GCC编译的标准C

当我运行这个程序并在提示符下输入一个字符时,我也必须按
ENTER键。这给了我2个输入字符 - ''a''和
''\ n''(Hex 61和0a)

好像getchar()函数需要ENTER来终止阅读
stdin。

当我只按一个键时,我试图让程序响应(即
而不需要按ENTER键)。

程序
~~~~~~~
#include< stdio.h>
#include< stdlib.h>

int main ()


如果函数没有参数,最好明确一下。

int main(void)

{
char x; //输入字符
while(1)
{
printf(">"); //打印提示''>''


你应该在这里刷新stdout,你的提示可能不会显示。

x = getchar(); //获取输入字符


getchar可以返回EOF,表示文件结束或错误。大多数系统

都有一种让用户通过键盘发出文件结束信号的方式,所以即使你的程序只是以交互方式使用,这仍然会发生。得到

将结果输入int(不是char)类型的变量并测试EOF。

printf("%d%02x \ n",x, x); //以十进制打印字符&
Hex
if(x ==''q'')//如果字符为q则退出
退出(0 );
}

输出
~~~~~
I am using Standard C compiled with GCC under Linux Fedora Core 4

When I run this program and enter a character at the prompt, I have to
press
the ENTER key as well. This gives me 2 input characters - ''a'' and
''\n'' (Hex 61 and 0a)

It seems as though the getchar() function needs ENTER to terminate reading
stdin.

I am trying to get the program to respond when I press one key only (ie
without needing to press ENTER as well).

Program
~~~~~~~
#include <stdio.h>
#include <stdlib.h>

int main()
If a function does not take parameters it is better to be explicit about it.
int main(void)
{
char x; //Input character
while (1)
{
printf(">"); //Print prompt ''>''
You should flush stdout here your your prompt might not be displayed.
x = getchar(); //Get input character
getchar can return EOF indicating end-of-file or error. Most systems
have a way for the user to signal end-of-file from the keyboard, so even
if your program is only used interactively this can still happen. Get
the result in to a variable of type int (not char) and test for EOF.
printf("%d %02x \n", x, x);//Print character in decimal &
Hex
if (x == ''q'') //Exit if the character is ''q''
exit(0);
}
}

Output
~~~~~
a<输入字符


97 61


97 61

10 0a
b<输入字符
10 0a
b < input character


98 62


98 62

10 0a
c<输入字符
10 0a
c < input character


99 63


99 63

10 0a
d<输入字符
10 0a
d < input character


100 64


100 64

10 0a
q<输入字符
10 0a
q < input character


113 71

我尝试过使用scanf()但得到了相同的结果。

关于如何输入ONE的任何想法只有人物? TIA :)

Alan


113 71

I have tried using scanf() but got the same results.

Any ideas as to how I can input ONE character only? TIA :)

Alan



在网上搜索kbhit.c那里有一个适用于Linux的版本,我
使用它很多,它解决了这些问题。只需添加kbhit.c



Search the net for kbhit.c there''s a version of it out there for linux, i
use it a lot and it solves these kinds of problems. Just add the kbhit.c




< snip>


实现它都不可移植。如果你想讨论如何做到b
请把它带到一个适当的系统特定组。


getchar的工作原理就是它stdin的输入是

通常行缓冲(即输入被加载到一个缓冲区,直到

是一个完整的行,然后只有程序可用)。 />

此外,如果用户将输入重定向到来自文件的
,您希望发生什么?或管道输出另一个程序?使用由Eric建议的kbhit

解决方案可能会成为一个问题。

-

Flash Gordon,生活在有趣的时代。

网站 - http://home.flash-gordon。 me.uk/

comp.lang.c发布指南和介绍:
http://clc-wiki.net/wiki/Intro_to_clc


Alan写道:

我在Linux Fedora Core 4下使用GCC编译的标准C

当我运行这个程序并在提示符下输入一个字符时,我必须按下也输入密钥。这给了我2个输入
字符 - ''''和''\ n''(Hex 61和0a)

好像getchar()函数需要ENTER来
终止读取stdin。

当我只按一个键时,我试图让程序响应(即不需要按ENTER键)。

I am using Standard C compiled with GCC under Linux Fedora Core 4

When I run this program and enter a character at the prompt, I
have to press the ENTER key as well. This gives me 2 input
characters - ''a'' and ''\n'' (Hex 61 and 0a)

It seems as though the getchar() function needs ENTER to
terminate reading stdin.

I am trying to get the program to respond when I press one key
only (ie without needing to press ENTER as well).




只需考虑一下事情。通常,您希望能够即时输入和更正输入。这意味着使用

之类的东西,比如退格键。你想要在键盘和你的程序之间完成这个编辑工作

。这意味着

是一种信号发送方式编辑完成,使用此功能。这就是

ENTER键,我们老朋友更了解返回。


现在想想这是怎么做到的,你呢将看到缓冲输入一词来自哪里。


有些系统会绕过所有这些。他们使用

非标准功能,因此不在此处。讨论

他们去一个讨论你的特定系统的新闻组。


-

一些信息链接:

news:news.announce.newusers
http:// www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html



Just think about things for a moment. In general, you want to be
able to type, and correct, input on the fly. This means using
things like the backspace key. You want this editing to be done up
front, between the keyboard and your program. This means there has
to be a means of signalling "Editing done, use this". That is the
ENTER key, better known to us old fogies as ''return''.

Now think about how this is actually done, and you will see where
the term ''buffered input'' comes from.

Some systems make provisions for bypassing all this. They use
non-standard features, and are thus off topic here. For discussion
of them go to a newsgroup that discusses your particular system.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html


这篇关于getchar()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆