本文介绍了用户输入停止while循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行脚本的程序。如果用户键入script

myScript.dat,程序将从文本文件中获取命令,验证

正确性,并开始执行脚本UNTIL ...


我需要一种方法来停止执行用户输入。我在想

这样的东西:


while(用户没有按下''任意键''){

keepExecutingScript();

}


我怎么能做用户没有按下''任何键''这个逻辑?

运行Linux Red Hat V3。


谢谢,

I have a program that runs scripts. If the user types "script
myScript.dat" the program will grab commands from the text file, verify
correctness, and begin executing the script UNTIL...

I need a way to stop the execution with user input. I was thinking
something like this:

while(user hasn''t pressed ''any key''){
keepExecutingScript();
}

How can I do the "user hasn''t pressed ''any key'' " of this logic?
Running Linux Red Hat V3.

Thanks,

推荐答案




这在标准C中无法做到;试试comp.unix.programmer


干杯

Michael

-

电子邮件:我是/ at / gmx / dot / de地址。



This cannot done in standard C; try comp.unix.programmer

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.






Brian


-

请为上下文引用足够的上一条消息。要从

Google执行此操作,请点击显示选项。并使用展开的

标题中显示的回复。


http://www.eskimo.com/~scs/C-faq/q19.1.html

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.





如果你被允许选择用户用来打断

计划的密钥,你很有可能做到这一点没有超出标准定义的



(这可能是也可能不是你想做的事情而不是使用

而不是特定于系统的东西。)


大多数实现都有一种方法可以在运行的
程序中引发SIGINT,所以你只需安装一个处理程序设置一个标志,指示

是时候停止,并在主循环中检查它。

(注意你安排SIGINT异步引发的方式

完全取决于你的系统,可能根本没有办法。

(所有使用的系统默认使用ctrl-C,但是

检查你的文档。)所以你实际上并没有获得可移植性

到不支持用户造成的SIGINT的系统,你只是获得了

没有必须更改代码才能在系统之间移动。)


--------

#include< signal.h>


volatile sig_atomic_t time_to_stop = 0;


void sigint_handler(int ignore)

{

time_to_stop = 1;

/ *如果用户不耐烦并中断,则恢复默认信号处理程序(通常是即时终止

终止)

再次

* /

信号(SIGINT,SIG_DFL);

}


void do_main_loop (some_args args)

{

void(* old_sighandler)(int);


old_sighandler = signal(SIGINT,sigint_handler) ;

if(old_sighandler == SIG_IGN)

{

/ *假设有理由忽略它,重新忽略* /

信号(SIGINT,SIG_IGN);

}

if(old_sighandler == SIG_ERR)

{

report_signal_installation_error();

abort_if_we_dont_want_to_continue_uninterruptible( );;

}


而(time_to_stop == 0)

{

keep_executing_script(args );

}

do_any_cleanup_required();


/ *根据接下来的情况,我们可能要恢复原始状态

处理程序:

* /

if(old_sighandler!= SIG_ERR)

signal(SIGINT,old_sighandler);

}

--------


请注意,您可以在异步处理程序中执行的操作信号

严重受限。在volatile类型的对象中存储值

sig_atomic_t保证按照您的预期进行操作(完全正确地存储值

并在下次出现时可用在信号处理程序之外检查了

,并使用signal()安装一个新的处理程序

,你正在处理的信号也是明确定义的。调用大多数库

函数是不安全的,检查和/或修改大多数对象是不安全的,

并且真的不是很多你可以这么做而不试图这两个中的一个或另一个。

dave


-

Dave Vandervies


不要在这里写troglodyte,它会惹恼当地人。

- 劳伦斯柯比在comp.lang.c



If you''re allowed to choose which key the user uses to interrupt the
program, there''s a good chance you can do this without going beyond what
the standard defines.
(This may or may not be something you''d want to do instead of using
something system-specific instead.)

Most implementations have a way to cause SIGINT to be raised in a running
program, so you can just install a handler that sets a flag indicating
it''s time to stop, and check that in your main loop.
(Note that the way you arrange for SIGINT to be raised asynchronously
is entirely dependent on your system and there may not be a way at all.
(All of the systems that used nontrivially use ctrl-C by default, but
check your documentation.) So you''re not actually gaining portability
to systems that don''t support user-caused SIGINT, you''re just gaining
not having to change the code to move between systems that do.)

--------
#include <signal.h>

volatile sig_atomic_t time_to_stop=0;

void sigint_handler(int ignored)
{
time_to_stop=1;
/*Revert to the default signal handler (usually immediate
termination) for if the user gets impatient and interrupts
again
*/
signal(SIGINT,SIG_DFL);
}

void do_main_loop(some_args args)
{
void (*old_sighandler)(int);

old_sighandler=signal(SIGINT,sigint_handler);
if(old_sighandler==SIG_IGN)
{
/*Assume it was ignored for a good reason, re-ignore*/
signal(SIGINT,SIG_IGN);
}
if(old_sighandler==SIG_ERR)
{
report_signal_installation_error();
abort_if_we_dont_want_to_continue_uninterruptible( );
}

while(time_to_stop==0)
{
keep_executing_script(args);
}
do_any_cleanup_required();

/*Depending on what comes next, we may want to restore the original
handler:
*/
if(old_sighandler!=SIG_ERR)
signal(SIGINT,old_sighandler);
}
--------

Note that the things you can do in a handler for an asynchronous signal
are seriously limited. Storing a value in an object of type volatile
sig_atomic_t is guaranteed to do what you''d expect (store the value
completely and correctly and become available the next time it''s checked
outside the signal handler), and using signal() to install a new handler
for the signal you''re handling is also well-defined. Calling most library
functions isn''t safe, examining and/or modifying most objects isn''t safe,
and there''s really not a whole lot that you can do without trying to do
one or the other of those.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca

Don''t write in troglodyte here, it annoys the natives.
--Lawrence Kirby in comp.lang.c


这篇关于用户输入停止while循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:41