#include <cstring> #include <cerrno> #include <iostream> #include <string> #include <unistd.h> #include <sys/select.h> using namespace std; int getInput(string &in, const int timeout); int main() { string input(""); int ret; ret = getInput(input, 10); if (ret > 0) { cout << "Got you: " << input << endl; } else if (ret == 0) { cout << "timeout !" << endl; } else { cout << "something wrong: " << strerror(errno) << endl; } return 0; } int getInput(string &in, const int timeout) { fd_set rset; timeval tv; int ret; FD_ZERO(&rset); FD_SET(STDIN_FILENO, &rset); tv.tv_sec = timeout; tv.tv_usec = 0; ret = select(STDIN_FILENO + 1, &rset, NULL, NULL, &tv); if (ret > 0) getline(cin, in); return ret; } |
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |