实现从文件中检索 n 个字节的(稳定)方法时遇到问题

发布于 2024-10-28 08:18:19 字数 300 浏览 2 评论 0原文

我正在开发的库的目的之一是从文件中检索指定数量的字节,在这种特定情况下,我希望访问 /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

‖放下 2024-11-04 08:18:19

定义“标准”。您是指 ISO C 标准吗? POSIX? Linux 标准库 (LSB)?对于 POSIX,read 调用可让您指定缓冲区的大小你正在尝试阅读。您可以使用 pselectpoll 确定是否有字节可供读取,超时而不是阻塞。在 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.

执着的年纪 2024-11-04 08:18:19

试试这个

这是我认为可以解决您的问题的函数的手册页。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文