我想getline()在c ++中从命令行读取一个字符串.
为此,我想添加5秒的计时器.如果没有读取字符串,则程序将终止.
我怎样才能做到这一点?
我想在C++中实现超时功能.
如果用户未在2秒内输入该值,则程序必须显示超时语句并再次询问输入
EX(输出屏幕):
Timer=0;
Please enter the input: //if input is not given within 2 seconds then
Time-out: 2 seconds
Timer again set to 0
Please enter the input: //if input is not given within 2 seconds then
Time-out: 2 seconds
Timer again set to 0
Please enter the input:22
Data accepted
Terminate the program`
Run Code Online (Sandbox Code Playgroud)
码:
#include<iostream>
#include<time.h>
using namespace std;
int main()
{
clock_t endwait;
endwait = 2000 ;
cout<<endwait;
while (clock() < endwait)
{
cout<<"Please enter the input:"; …Run Code Online (Sandbox Code Playgroud) 简短的问题:我有一个实时模拟,它作为一个背景过程运行,并与管道连接到调用pogramm.我想使用stdin向该进程发送命令,以通过stdout从中获取某些信息.现在因为它是一个实时过程,它必须是一个非阻塞输入.boost :: asio :: async_read和iostream :: cin一起使用这个任务是个好主意吗?如果可行,我该如何使用该功能?还有什么建议吗?
我们试图从连接到linux盒子的2 usb鼠标中读取数据(这些数据用于机器人的测距/定位).所以我们需要不断地从每只鼠标中读取它移动了多少.问题是当鼠标没有移动时,它不会发送任何数据,因此我们从中获取数据块的文件流,因此程序无法进行里程计算(其中涉及速度的时间测量) .
有没有办法在输入流上设置超时(我们在C++中使用ifstream并从/ dev/input/mouse读取),这样我们就可以知道鼠标何时不移动,而不是等待要收到的活动?或者我们需要搞乱线程(arggh ......)?欢迎任何其他建议!
提前致谢!