These functions return the number of input items successfully
matched and assigned, which can be fewer than provided for, or even
zero in the event of an early matching failure. The value EOF
is returned if the end of input is reached before either the first
successful conversion or a matching failure occurs. EOF is also
returned if a read error occurs, in which case the error indicator for
the stream (see ferror(3)) is set, and errno is set indicate the error.
举例:
1. 如果一次输入的数据超过一个,它会自动分配给下一次的cin或者scanf,不会自动取第一个数字 > vi io.c "io.c" [New file] #include int main(void){ int a,b,c; printf("input a\n"); scanf("%d",&a); printf("input b\n"); scanf("%d",&b); printf("input c\n"); scanf("%d",&c); printf("%d,%d,%d\n"); return 0; }
> vi io.cpp #include using namespace std; int main(void){ int a,b,c; cout<<"input a\n"; cin>>a; cout<<"input b\n"; cin>>b; count< cin>>c; cout< return 0; }
int main(void) { char c; ungetc('a',stdin); scanf("%c",&c); printf("%c\n",c); }
>./a.out
a (没有交互)
如果scanf失败了,下次scanf还是会取缓存里面的内容,而不理会是否需要用户输入了。一种改进的方法是: #include int main(void) { int i=1101; printf("\r\nPlz input an interger number: \r\n"); int r=scanf("%d", &i); if (i < 1 || i > 100) { printf("\r\nERROR: Your choice must be between 1 to 100!\n"); } printf("\r\nINFO: Your choice is %d!\n", i); if(r)while(getchar()!='\n');//解决问题!!!!!!!!!!!!!!! scanf("%d", &i); printf("\r\nINFO: Your choice is %d!\n", i); return 0; }
(2)运行结果: a进程运行以后对文件加锁,打印"lock got",然后睡20s,结束 b进程启动以后fcntl会等待(因为F_SETLKW),然后第5s的时候,进程接受到了SIGALRM,(因为alarm(5)的原因,然后打印 signal handle: 14 error=4,Interrupted system call
------------------------------------------------------------------------ NAME sleep - suspend execution for an interval of time
SYNOPSIS #include unsigned sleep(unsigned seconds); DESCRIPTION
The sleep() function shall cause the calling thread to be suspended
from execution until either the number of realtime seconds specified by
the argument seconds has elapsed or a signal is delivered to the
calling thread and its action is to invoke a signal-catching function
or to terminate the process. The suspension time may be longer than
requested due to the scheduling of other activity by the system.
If a SIGALRM signal is generated for the calling process during
execution of sleep() and if the SIGALRM signal is being ignored or
blocked from delivery, it is unspecified whether sleep() returns when
the SIGALRM signal is scheduled. If the signal is being blocked, it is
also unspecified whether it remains pending after sleep() returns or it
is discarded. If a SIGALRM signal is generated for the calling
process during execution of sleep(), except as a result of a prior call
to alarm(), and if the SIGALRM signal is not being ignored or blocked
from delivery, it is unspecified whether that signal has any effect
other than causing sleep() to return. If a signal-catching
function interrupts sleep() and examines or changes either the time a
SIGALRM is scheduled to be generated, the action associated with the
SIGALRM signal, or whether the SIGALRM signal is blocked from delivery,
the results are unspecified. If a signal-catching function
interrupts sleep() and calls siglongjmp() or longjmp() to restore an
environment saved prior to the sleep() call, the action associated with
the SIGALRM signal and the time at which a SIGALRM signal is scheduled
to be generated are unspecified. It is also unspecified whether the
SIGALRM signal is blocked, unless the process' signal mask is restored
as part of the environment. RATIONALE There are two general
approaches to the implementation of the sleep() function. One is to use
the alarm() function to schedule a SIGALRM signal and then suspend the
calling thread waiting for that signal. The other is to implement an
independent facility. This volume of IEEE Std 1003.1-2001 permits
either approach. In order to comply with the requirement that no
primitive shall change a process attribute unless explicitly described
by this volume of IEEE Std 1003.1-2001, an implementation using SIGALRM
must carefully take into account any SIGALRM signal scheduled by
previous alarm() calls, the action previously established for SIGALRM,
and whether SIGALRM was blocked. If a SIGALRM has been scheduled before
the sleep() would ordinarily complete, the sleep() must be shortened to
that time and a SIGALRM generated (possibly simulated by direct
invocation of the signal-catching function) before sleep() returns. If
a SIGALRM has been scheduled after the sleep() would ordinarily
complete, it must be rescheduled for the same time before sleep()
returns. The action and blocking for SIGALRM must be saved and restored.
Historical implementations often implement the SIGALRM-based version
using alarm() and pause(). One such implementation is prone to infinite
hangups, as described in pause(). Another such implementation uses the
C-language setjmp() and longjmp() functions to avoid that window. That
implementation introduces a different problem: when the SIGALRM signal
interrupts a signal-catching function installed by the user to catch a
different signal, the longjmp() aborts that signal-catching function.
An implementation based on sigprocmask(), alarm(), and sigsuspend() can
avoid these problems.