FREAD / FWRITE大小和数量 [英] fread/fwrite size and count

查看:464
本文介绍了FREAD / FWRITE大小和数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对其中的参数大小顺序疑虑和计数用于FREAD / FWRITE。如果我想从文件中读取FP,下列哪项是更有效率?数据8KB

I have doubts about which order of the parameters size and count to use for fread/fwrite. If I want to read 8kb of data from file fp, which of the following is more efficient?

fread(data,1,8192,fp)
fread(data,8192,1,fp)

也是有字节序,我应该担心的问题?

Also are there endiannes issues that I should be worried about?

推荐答案

他们是完全等价的。至于字节序,这取决于你正在阅读的内容。通常情况下,这将是个字节,你会然后必须无格的缓冲器,根据与它们所写入的格式。而且,由于它的字节,字节序起到任何作用。

They're exactly equivalent. As for endianness, it depends on what you're reading. Normally, it will be a buffer of bytes, which you'll then have to "unformat", according to the format with which they were written. And since it's bytes, endianness plays no role.

编辑:

由于simonc指出,(然后将其删除,因为他没有得到它100%正确的---但他的观点是有效的):有与问候返回值(这需要以使用的差异要知道该功能是否工作与否)。 FREAD(缓冲,8192,1,FP)将返回0或1,只有当所有的8192个字节被读取1。此外,Posix的说,用于部分地读对象未指定的缓冲区的内容。在实践中,缓冲区将被填充的字节数可以被理解的,但因为你不知道有多少,这是,不买你多少。总之,你应该总是使用 FREAD(缓冲,1,8192,FP); (因为它没有意义,使用此功能什么,但字节的缓冲区)。

As simonc pointed out (and then deleted, because he didn't get it 100% right---but his point was valid): there is a difference with regards to the return value (which you need to use in order to know whether the function worked or not). fread( buffer, 8192, 1, fp ) will return either 0 or 1, and 1 only if all 8192 bytes have been read. In addition, Posix says that the contents of the buffer are not specified for partially read objects. In practice, the buffer will have been filled with as many bytes as could be read, but since you don't know how many that was, that doesn't buy you much. In sum, you should always use fread( buffer, 1, 8192, fp ); (since it makes no sense to use this function for anything but a buffer of bytes).

这篇关于FREAD / FWRITE大小和数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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