在移植的C如何寻求着从管道读取数据时 [英] How in portable C to seek forward when reading from a pipe

查看:130
本文介绍了在移植的C如何寻求着从管道读取数据时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 fseek的()不会对模拟寻求着存在哪些方法管工作?天真的方法是使用 FREAD()扔掉读入内存缓冲区的内容。对于庞大力求避免巨大的缓冲区,你会反复使用同一缓冲区仅仅用一个缓冲的一部分,最终使读取。

Since fseek() does not work on pipes what methods exist for simulating seeking forward? The naive approach is to use fread() and throw away the contents read into the memory buffer. For huge seeks to avoid huge buffers you would use the same buffer over and over with the final read using just a part of the buffer.

但是,这是唯一的方法?是否有避免了缓冲区和潜在的多个读取另一种方式?

But is this the only approach? Is there another way which avoids the buffer and the potential multiple read?

推荐答案

是的,这是唯一的出路。我会的地方使用缓冲区1K左右-8K。随着更小的系统调用开销读会发挥作用,并与更大的,​​你会从缓存中驱逐有用的数据。

Yes, it is the only way. I would use a buffer somewhere around 1k-8k. With much smaller the syscall overhead for read will come into play, and with much larger you'll evict useful data from the cache.

这篇关于在移植的C如何寻求着从管道读取数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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