确定管道的大小,而不调用阅读() [英] Determine the size of a pipe without calling read()

查看:158
本文介绍了确定管道的大小,而不调用阅读()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个叫做SizeOfPipe()函数应返回一个用户喉─我只想知道多少数据是在管道和实际上不脱管本身读数据的大小。

I need a function called SizeOfPipe() which should return the size of a pipe- I only want to know how much data is in the pipe and not actually read data off the pipe itself.

我觉得以下code将工作

I thought the following code would work

fseek (pPipe, 0 , SEEK_END);
*pBytes = ftell (pPipe);
rewind (pPipe);

但文件描述符fseek的dosent工作。另一种办法是阅读管道,然后将数据写回,但想如果possibe,有什么建​​议?

but fseek dosent work on file descriptors. Another option would be to read the pipe then write the data back but would like to avoid this if possibe, any suggestions ?

推荐答案

根据您的UNIX实现的ioctl / FIONREAD可能做的伎俩

Depending on your unix implementation ioctl/FIONREAD might do the trick


err = ioctl(pipedesc, FIONREAD, &bytesAvailable);

除非本返回错误code为无效的参数(或任何其他错误)信息bytesAvailable包含的数据可当时疏通读操作的数量。

Unless this returns the error code for "invalid argument" (or any other error) bytesAvailable contains the amount of data available for unblocking read operations at that time.

这篇关于确定管道的大小,而不调用阅读()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆