pipe(c++)中有多少数据
How much data is in pipe(c++)
我试图猜测管道中有多少数据,但我不想使用while(read)
因为它在EOF之前阻塞。
有什么办法可以做到这一点吗?
我真的我想要这样的东西:
i = pipe1.size();
pipe1.read(i);
我再说一遍,我不想使用while (read)
因为它在 EOF 之前会阻塞。
来自管道的数据量可能是无限的,就像一个流一样,管道中没有size
的概念。 如果您不希望它在没有任何可读取的内容时阻塞,则应在调用pipe2()
时设置O_NONBLOCK
标志:
pipe2(pipefd, O_NONBLOCK);
这样,当您调用read()
如果没有数据时,它将失败并将errno
设置为 EWOULDBLOCK
if (read(fd, ...) < 0) {
if (errno == EWOULDBLOCK) {
//no data
}
//other errors
}
从手册页:
O_NONBLOCK:在两个新打开的文件上设置O_NONBLOCK文件状态标志 文件说明。 使用此标志可以节省对 fcntl(2) 的额外调用 达到相同的结果。
您也可以在阻塞管道上使用 select() 来超时。
这可以帮助你,但它是特定于 unix 的:
#include <iostream>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <errno.h>
int pipe_fd; /* your pipe's file descriptor */
......
int nbytes = 0;
//see how much data is waiting in buffer
if ( ioctl(pipe_fd, FIONREAD, &nbytes) < 0 )
{
std::cout << "error occured: " << errno;
}
else
{
std::cout << nbytes << " bytes waiting in buffer";
}
相关文章:
- 在 C++ AMP 数组中复制数据多少次?
- 在 C++ 中,默认情况下构造函数为类的数据成员提供的值是多少?
- 如何用 c++ 读取 txt 文件,如果我们不知道其中有多少个数据字符串,以及每行有多长?
- 我可以通过命令行参数传递给C++程序多少数据?
- 一个HDF5文件中有多少个数据集
- 如何在C 中编写一个程序,以说明不知道它正在阅读的数据文本文件中包含多少行
- pipe(c++)中有多少数据
- 创建多个实例后,静态数据成员的值是多少
- 在C++中,如果我们不在构造函数中初始化数据成员,那么类中数据成员的值会是多少
- 哪些数据结构正在使用多少内存
- 一个向量可以容纳多少数据<string>?
- 如何返回实际消耗了多少字节来从inflate()返回解压缩的数据
- 你可以在堆栈中放入多少数据
- 增强变体支持的数据类型有多少?
- 在c++中,枚举数据类型消耗多少内存
- 应该为使用GetDIBits函数从HBITMAP接收的DIB数据分配多少内存
- const引用右值的类数据成员的生命周期是多少?
- c++数据结构对象的生命周期是多少?
- ID3D11Buffer中可以存储的最大数据大小是多少?
- 在C++中使用Templates时,编译器如何知道要实例化多少数据类型