读取终端输入的字符,当未读到数据时延时3秒,而后再读:string
#include
#include
#include
#include
#include
#include
#include
#include it
/************************************************************
************************************************************/
int main(int argc, char* args[])
{
int fd1 = 0;
int len = 0;
char buf[10];io
fd1 = open("/dev/tty",O_RDONLY|O_NONBLOCK);
if(0>fd1)
{
perror("open /dev/tty error");
exit(1);
}终端
write(STDOUT_FILENO,"Please enter a string!\n",23);
do
{
len = read(fd1,buf,10);
if(0<=len)
{
break;
}
else
{
sleep(3);
write(STDOUT_FILENO,"try again!\n",11);
}
}while( (0>len)&&(EAGAIN==errno) );gc
if(0
{
write(STDOUT_FILENO,"Your enter string is:\n",22);
write(STDOUT_FILENO,buf,len);
write(STDOUT_FILENO,"\n",1);
}error
close(fd1);
return(0);
}
数据