I would like to read some data from a stream I have using std::getline.
Below a sample using the std::cin.
std::string line;
std::getline( std::cin, line );
This is a blocking function i.e. if there is no data or line to read it blocks execution.
Do you know if exists a function for checking data availability before calling std::getline? I don’t want to block.
How can I check whether the stream buffer is full of data valid for a successful call to std::getline?
Whatever looks like the code below
if( dataAvailableInStream() )
{
std::string line;
std::getline( std::cin, line );
}
The
iostreamlibrary doesn’t support the concept of non-blocking I/O. I don’t think there’s anything in the C++ standard that does. Any good solution would likely be platform-specific. If you can use the POSIX libraries, you might look intoselect. It’s usually used for networking stuff, but it’ll work just fine if you pass it the file descriptor for stdin.