5

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?

danny
  • 1,101
  • 1
  • 12
  • 34

2 Answers2

11

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.

EDIT:

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).

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • Well, since it's bytes, obviously endianness will play some role. Imagine he is trying to read these 8kb of data into a structure which has `int` members, for example. You are assuming he is trying to do something like `char string[] = fread(data,8192,1,fp);` – fvdalcin Oct 16 '13 at 17:51
  • 4
    @fvdalcin He's reading bytes. Bytes don't have endianness. Once he's read them, he has to interpret them according to the specifications of the file format. That should normally be done without considerations of endianness as well. – James Kanze Oct 16 '13 at 17:53
  • @fvdalcin And yes, the only reasonable use of these functions is `unsigned char buffer[8192]; fread( buffer, 8192, 1, fp );`. Afterwards, you have to interpret the buffer, according to the specified file format. – James Kanze Oct 16 '13 at 17:55
  • Thanks. Are they equally efficient? – danny Oct 16 '13 at 17:57
  • @danny Both will be limited by the speed of the disk accesses and transferring data from the OS into your process. I wouldn't worry about efficiency differences here; any differences will be negligible compared to everything else. – James Kanze Oct 16 '13 at 18:02
  • Ok great. I just realized I was using the wrong order and wondered if I had introduced major bugs. – danny Oct 16 '13 at 18:03
  • 1
    @James Kanze Just a note: `fread()` may be used with I/O other than disks & `stdin`, as in reading a partial 8192 block from serial I/O. – chux - Reinstate Monica Oct 16 '13 at 19:21
  • @chux But of course. But you still have to deal with the format of the data input. – James Kanze Oct 17 '13 at 08:19
3

They're not exactly equivalent.

fread() was mis-designed. Its design ignores the possibilty of a partial read at the end of the file, which cannot be expressed by its return value, which is in units of the size you specified.

You should really only use fread() where the size is 1 and the length is the number of bytes you're expecting. That's the only way you can properly deal with the situation. For types other than char that means providing sizeof char as the size and n*sizeof T as the length, where n is the number of Ts you are expecting.

user207421
  • 305,947
  • 44
  • 307
  • 483