麻烦从实施检索文件n个字符的(稳定)方法 [英] Trouble implementing a (stable) method of retrieving n bytes from file

查看:152
本文介绍了麻烦从实施检索文件n个字符的(稳定)方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中之一我开发库的目的是从一个文件中检索指定的字节数量,在这种特定的情况下,我希望访问的的/ dev /随机的检索基于熵的随机序列。

One of the purposes of the library of which I am developing is to retrieve a specified amount of bytes from a file, in this specific case I am wishing for access to /dev/random to retrieve entropy based random sequences.

我用fread主要问题是,在等待更多的熵时,将挂起无限期,这是不必要的。我的下一个选择会被包裹FREAD用的feof采取大块字节,那么我至少可以提供完整的百分比为更好的体验,虽然从我可以收集迭代1,2,3,4 ..'s字节会很难跟踪等于如数必要的。

My main issue with fread is that it will hang indefinitely when waiting for more entropy, and this is unwanted. My next choice would have been wrapping fread with feof to take bytes in chunks, then I could at least provide percentages complete for a better experience, although from what I could gather iteration 1, 2, 3, 4..'s bytes will be hard to track to equal exactly the amount needed.

有没有在C标准,允许什么,我要寻找一个方法,确切数额要和块?如果我要寻找的这种超时,将线程数据请求是看一个不错的选择?

Is there a method in a C standard that allows for what I am looking for, exact amount wanted and in chunks? If I were to look for timeouts of this, would threading the data request be a good option to look at?

推荐答案

定义的标准。你的意思是ISO C标准? POSIX? Linux标准基础(LSB)?对于POSIX的阅读通话,可以指定缓冲区的大小你想阅读。您可以使用 PSELECT 或的轮询以确定是否有可用的要读取的字节,以超时,而不是阻断。在Linux上,可以使用FIONREAD的ioctl 来电获取可用的字节的确切数字阅读。

Define "standard". Do you mean the ISO C standard? POSIX? Linux standards base (LSB)? For POSIX, the read call lets you specify the size of the buffer that you are trying to read. You can use pselect or poll to determine if there are bytes available to be read, with a timeout instead of blocking. On Linux, it is possible to use the "FIONREAD" ioctl call to obtain the exact number of bytes available for reading.

这是说,你应该问自己,如果你需要熵这一水平。你可能(也可能不会)能够从的/ dev / urandom的读书脱身。当然,你必须确定是否是这种情况。

That said, you should ask yourself if you need that level of entropy. You might (or might not) be able to get away with reading from "/dev/urandom". Of course, you would have to determine if that is the case.

这篇关于麻烦从实施检索文件n个字符的(稳定)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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