在调用std :: getline之前检查数据可用性 [英] checking data availability before calling std::getline
问题描述
我想从流中使用 std :: getline
读取一些数据。
下面使用 std :: cin
的示例。
std :: string line;
std :: getline(std :: cin,line);
这是一个阻塞函数,如果没有数据或行读取它阻止执行。 p>
你知道在调用 std :: getline
之前是否存在检查数据可用性的函数?我不想阻止。
如何检查流缓冲区是否填满了有效成功调用 std :: getline的数据
无论如何代码如下
if(dataAvailableInStream ))
{
std :: string line;
std :: getline(std :: cin,line);
}
iostream
库不支持非阻塞I / O的概念。我认为C ++标准中没有任何东西。任何好的解决方案都可能是平台特定的。如果您可以使用POSIX库,则可以查看 select
。它通常用于网络的东西,但它会工作很好,如果你传递的stdin的文件描述符。
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 iostream
library 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 into select
. It's usually used for networking stuff, but it'll work just fine if you pass it the file descriptor for stdin.
这篇关于在调用std :: getline之前检查数据可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!