如何清除什么scanf函数读? [英] How can I clear what scanf read?
问题描述
我希望能够明确什么scanf函数读入的值。换句话说,我想删除由scanf函数读取值。
下面是我的示例code:
的#include<&stdio.h中GT;
#包括LT&;&signal.h中GT;
#包括LT&; SYS / time.h中>挥发性sig_atomic_t gotsignal;
空隙的处理程序(){gotsignal = 1;}诠释主(){结构sigaction的签名;sig.sa_handler =处理程序;
sig.sa_flags = 0;
sigemptyset(安培; sig.sa_mask);报警器(5);
的sigaction(SIGALRM,&安培; SIG,NULL);
int值;而(!gotsignal){
的printf(插入值:\\ n);
scanf函数(%d个,&安培;值);
}
}
输出:
插入一个值:
5(不preSS进入)[JPS @本地℃] 5℃$; -
是否有可能(如果是如何)清除5?
我一直在阅读终端设置,fflushs,标准输入,但我无法弄清楚。任何帮助吗?
编辑:经过大量的改掉我想我找到一些作品。如果任何人有这个问题,这个工作对我来说(不知道这是否适用于其他系统和东西,还挺新本):
的#include<&stdio.h中GT;
#包括LT&;&signal.h中GT;
#包括LT&; SYS / time.h中>
#包括LT&;&termios.h GT;
#包括LT&;&unistd.h中GT;
挥发性sig_atomic_t gotsignal;
无效的处理程序()
{gotsignal = 1;}
诠释主(){结构sigaction的签名;sig.sa_handler =处理程序;
sig.sa_flags = 0;
sigemptyset(安培; sig.sa_mask);报警器(5);
的sigaction(SIGALRM,&安培; SIG,NULL);
int值;
而(!gotsignal){
的printf(插入值:\\ n);
scanf函数(%d个,&安培;值);
}
的printf(\\ n);
tcflush(STDOUT_FILENO,TCIOFLUSH); < -important位!返回0;}
输出:
插入一个值:
五
插入值:
5(不进入是pressed)!
[JPS @本地℃] $< - 没有更多的NR 5! :D
您可能使用的的readline 库。但我不知道理解你所说的清除5的意思。
最简单的例子可能是
//文件testrl.c
#包括LT&;的readline / readline.h>
#包括LT&;&stdlib.h中GT;诠释的main()
{
烧焦bufprompt [20];
字符*林= NULL;
INT CNT = 0;
为(;;){
memset的(bufprompt,0,sizeof的(bufprompt));
CNT ++;
的snprintf(bufprompt,sizeof的(bufprompt)-1,%D:,CNT);
林=的ReadLine(bufprompt);
如果(!林)
打破;
的printf(你输入%S \\ n,林);
免费(LIN);
}
返回0;
}
编译上面的gcc -Wall -g -o testrl.c testrl -lreadline
又见<一个href=\"http://stackoverflow.com/questions/8242404/detect-meta-special-keys-ctrl-shift-alt-tab-esc-backspace-from-shell-in/8244551#8244551\">this问题
I want to be able to clear the value of what scanf read in. In other words, I want to delete the value that was read in by scanf.
Here is my sample code:
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
volatile sig_atomic_t gotsignal;
void handler(){
gotsignal = 1;
}
int main(){
struct sigaction sig;
sig.sa_handler = handler;
sig.sa_flags = 0;
sigemptyset(&sig.sa_mask);
alarm(5);
sigaction(SIGALRM, &sig, NULL);
int value;
while(!gotsignal){
printf("Insert a value: \n");
scanf("%d", &value);
}
}
Output:
Insert a value:
5(dont press enter)[JPS@localhost c]$ 5 <-
Is it possible(if yes how) to clear the 5?
I have been reading about terminal settings, fflushs, stdin, but i couldn't figure it out. Any help please?
EDIT: After a lot of trys i think i found something that works. If anyone has this problem this worked for me(not sure if it works on other systems and stuff, kinda new to this):
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
volatile sig_atomic_t gotsignal;
void handler()
{
gotsignal = 1;
}
int main(){
struct sigaction sig;
sig.sa_handler = handler;
sig.sa_flags = 0;
sigemptyset(&sig.sa_mask);
alarm(5);
sigaction(SIGALRM, &sig, NULL);
int value;
while(!gotsignal){
printf("Insert a value: \n");
scanf("%d", &value);
}
printf("\n");
tcflush(STDOUT_FILENO,TCIOFLUSH); <-important bit!
return 0;
}
Output:
Insert a value:
5
Insert a value:
5(no enter was pressed)!
[JPS@localhost c]$ <- NO MORE NR 5! :D
You might use the readline library. But I'm not sure to understand what you mean by "clear the 5".
The simplest example might be
// file testrl.c
#include <readline/readline.h>
#include <stdlib.h>
int main ()
{
char bufprompt[20];
char* lin = NULL;
int cnt = 0;
for (;;) {
memset(bufprompt, 0, sizeof(bufprompt));
cnt++;
snprintf(bufprompt, sizeof(bufprompt)-1, "%d: ", cnt);
lin = readline(bufprompt);
if (!lin)
break;
printf("you typed %s\n", lin);
free (lin);
}
return 0;
}
Compile the above with gcc -Wall -g testrl.c -o testrl -lreadline
See also this question
这篇关于如何清除什么scanf函数读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!