How to make reading from `std::cin` timeout after a particular amount of time

后端 未结 3 443
别那么骄傲
别那么骄傲 2020-11-27 22:40

I have written a small program,

int main(int argc, char *argv[])
{
    int n;
    std::cout << \"Before reading from cin\" << std::endl;

    //          


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 23:04

    I have a bad news for you: cin is NOT A STATEMENT. It is an object of type std::istream that remaps the standard input file that your OS maps by default to your program's console.

    What blocks is not cin, but the console line editor that the console itself invokes when the standard input is read with an empty buffer.

    What you are asking goes ahead of the standard input model cin is supposed to wrap, and cannot be implemented as a istream functionality.

    The only clean way to do it is using the native I/O functionality of the console, to get user events, and -eventually- rely on C++ streams only after you've got some characters to be parsed.

提交回复
热议问题