有linux系统的同学来测试下这个程序

liu7ra 2010-04-20 04:05:30
VTIME设置了timer,我在ubuntu9.10测试不行。注意timer在接收到一个字符时开始计时。
/* play_again3.c
* purpose: ask if user wants another transaction
* method: set tty into chr-by-chr, no-echo mode
* set tty into no-delay mode
* read char, return result
* returns: 0=>yes, 1=>no, 2=>timeout
* better: reset terminal mode on Interrupt
*/
#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>

#define ASK "Do you want another transaction"
#define TRIES 3 /* max tries */
#define BEEP putchar('\a') /* alert user */

main()
{
int response;

tty_mode(0); /* save current mode */
set_cr_noecho_mode(); /* set -icanon, -echo */
response = get_response(ASK, TRIES); /* get some answer */
tty_mode(1); /* restore orig mode */
return response;
}
get_response( char *question , int maxtries)
{
int input;

printf("%s (y/n)?", question); /* ask */
fflush(stdout); /* force output */
while ( 1 ){
input = tolower(get_ok_char()); /* get next chr */
if ( input == 'y' )
return 0;
if ( input == 'n' )
return 1;
if ( maxtries-- == 0 ) /* outatime? */
return 2; /* sayso */
BEEP;perror("\nHello?\n");
}
}

get_ok_char()
{
int c;
while( ( c = getchar() ) != EOF && strchr("yYnN",c) == NULL )
;
return c;
}

set_cr_noecho_mode()

{
struct termios ttystate;

tcgetattr( 0, &ttystate); /* read curr. setting */
ttystate.c_lflag &= ~ICANON; /* no buffering */
ttystate.c_lflag &= ~ECHO; /* no echo either */
ttystate.c_cc[VMIN] = 1; /* get 1 char at a time */
ttystate.c_cc[VTIME] = 20;
tcsetattr( 0 , TCSANOW, &ttystate); /* install settings */
}

set_nodelay_mode()

{
int termflags;

termflags = fcntl(0, F_GETFL); /* read curr. settings */
termflags |= O_NDELAY; /* flip on nodelay bit */
fcntl(0, F_SETFL, termflags); /* and install 'em */
}


tty_mode(int how)
{
static struct termios original_mode;
if ( how == 0 )
tcgetattr(0, &original_mode);
else
tcsetattr(0, TCSANOW, &original_mode);
}
...全文
292 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
thunder_2011 2011-06-22
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 bokutake 的回复:]

引用 6 楼 zhao4zhong1 的回复:

get_ok_char()
{
int c;
fflush(stdin);//加这句
while( ( c = getchar() ) != EOF && strchr("yYnN",c) == NULL )
;
return c;
}


这位,你不知道fflush(stdin)是未定义行为么?只有在TC……
[/Quote]

用fflush 确实不能刷新stdin,一直都用 while(getchar() != '\n') ,不知道 while ( ( c = getchar() ) != EOF && c != '\n' ) 这样用是为何? 又不是读文件为什么要判断 EOF呢?getchar返回 -1 是什么情况?
最后一个菜鸟 2011-06-21
  • 打赏
  • 举报
回复
为什么在11.04下就算加上那一句也不行啊
xingzhiyaoni321 2011-06-21
  • 打赏
  • 举报
回复
setbuf(stdin,NULL);//一样清空缓冲区,感觉适用点,
c__sys 2011-06-21
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 bokutake 的回复:]

引用 6 楼 zhao4zhong1 的回复:

这位,你不知道fflush(stdin)是未定义行为么?只有在TC……
[/Quote]

那位就是专贴“VC……F5……汇鞭”狗屁膏药的牛人
辰岡墨竹 2011-06-21
  • 打赏
  • 举报
回复
也可以用setvbuf禁用输入缓冲(_IONBF),或者setbuf将输入缓冲大小设置为0.
辰岡墨竹 2011-06-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zhao4zhong1 的回复:]

get_ok_char()
{
int c;
fflush(stdin);//加这句
while( ( c = getchar() ) != EOF && strchr("yYnN",c) == NULL )
;
return c;
}
[/Quote]

这位,你不知道fflush(stdin)是未定义行为么?只有在TC、VC里等C库里才有清除输入缓冲效果,Linux下可不能那么用。rewind(stdin)同样也是未定义行为。
规范的清除stdin的方法是:
标准方法是:
int c;
while ( ( c = getchar() ) != EOF && c != '\n' )
;
或者
scanf ( "%*[^\n]" ); // 吃掉除了回车以外所有字符
scanf ( "%*c" ); // 吃掉回车
2011-06-21
  • 打赏
  • 举报
回复
打错字了……
VIM ==> VMIN
2011-06-21
  • 打赏
  • 举报
回复
据说用 VTIME 的时候 VIM 要等于 0。

ttystate.c_lflag &= ~ICANON; /* no buffering */
ttystate.c_lflag &= ~ECHO; /* no echo either */
ttystate.c_cc[VMIN] = 0; /* get 1 char at a time */
ttystate.c_cc[VTIME] = 20;
赵4老师 2011-06-21
  • 打赏
  • 举报
回复
get_ok_char()
{
int c;
rewind(stdin);//加这句
while( ( c = getchar() ) != EOF && strchr("yYnN",c) == NULL )
;
return c;
}
c__sys 2011-06-21
  • 打赏
  • 举报
回复


[Quote=引用 6 楼 zhao4zhong1 的回复:]

get_ok_char()
{
int c;
fflush(stdin);//加这句
while( ( c = getchar() ) != EOF && strchr("yYnN",c) == NULL )
;
return c;
}
[/Quote]


stupid mother fucker, fflush can't be used on input stream.

this is undefined behavior:
— The stream for the fflush function points to an input stream or to an update stream
in which the most recent operation was input (7.21.5.2).

/* clreol - removes all characters from a stream up to and including
* the next newline character. If the end of the stream is encountered,
* this function returns 1. Otherwise it returns 0.
* Richard Heathfield May 15 2007, 4:57 pm
*/
#include <stdio.h>
int clreol(FILE *fp)
{
int ch;
while((ch = getc(fp)) != EOF && ch != '\n')
{
continue;
}
return ch == EOF;
}

最后一个菜鸟 2011-06-20
  • 打赏
  • 举报
回复
当没有输入字符时,while的条件判断为假吗
liu7ra 2010-05-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zhao4zhong1 的回复:]

get_ok_char()
{
int c;
fflush(stdin);//加这句
while( ( c = getchar() ) != EOF && strchr("yYnN",c) == NULL )
;
return c;
}
[/Quote]正解了,多谢!
liu7ra 2010-04-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 liujiaji 的回复:]

运行了,有啥问题吗?
[/Quote]是我提问题没有说明白...
不输入y和n 会延时两秒,重复3次,然后程序自动结束。但是我测试不成功
eTouX 2010-04-22
  • 打赏
  • 举报
回复
标准输入输出总是会出现这些令人不喜欢的事
赵4老师 2010-04-22
  • 打赏
  • 举报
回复
get_ok_char()
{
int c;
fflush(stdin);//加这句
while( ( c = getchar() ) != EOF && strchr("yYnN",c) == NULL )
;
return c;
}
spygg 2010-04-22
  • 打赏
  • 举报
回复
我测试了也不成功,只有一次
liujiaji 2010-04-20
  • 打赏
  • 举报
回复
运行了,有啥问题吗?
spygg 2010-04-20
  • 打赏
  • 举报
回复
运行结果:Do you want another trasation(y/n)?
fairuyy 2010-04-20
  • 打赏
  • 举报
回复
没有Linux....

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧