实现从文件中检索 n 个字节的(稳定)方法时遇到问题
我正在开发的库的目的之一是从文件中检索指定数量的字节,在这种特定情况下,我希望访问 /dev/random 来检索基于熵的随机序列。
我对 fread 的主要问题是,当等待更多熵时,它会无限期地挂起,这是不需要的。我的下一个选择是用 feof 包装 fread 以将字节分成块,然后我至少可以提供完整的百分比以获得更好的体验,尽管从我可以收集的迭代 1、2、3、4.. 的字节将是很难准确追踪所需的数量。
C 标准中是否有一种方法可以满足我正在寻找的内容、所需的确切数量和块?如果我要寻找超时情况,线程化数据请求是否是一个不错的选择?
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.
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
定义“标准”。您是指 ISO C 标准吗? POSIX? Linux 标准库 (LSB)?对于 POSIX,read 调用可让您指定缓冲区的大小你正在尝试阅读。您可以使用 pselect 或 poll 确定是否有字节可供读取,超时而不是阻塞。在 Linux 上,可以使用“FIONREAD” ioctl 调用获取可供读取的确切字节数。
也就是说,你应该问自己是否需要那种程度的熵。您可能(或可能不会)能够从“/dev/urandom”中读取内容。当然,您必须确定是否是这种情况。
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.
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.
试试这个
这是我认为可以解决您的问题的函数的手册页。
http://www.manpagez.com/man/3/fgets/
我只是看到 fread 不起作用,fgets 从文件流读取一定数量的字节到缓冲区
Try this
Here is the man page for a function I think will solve your problem.
http://www.manpagez.com/man/3/fgets/
I just saw that fread wasnt working, fgets reads a certain number of byes from file stream into buffer