If I use:
fread(&buffer, 16384, 1, file); // Read 16384 bytes once?
instead of:
fread(&buffer, 1, 16384, file); // Read a byte 16384 times?
to read a plain text document file, would it succeed? Is it efficient?
CodePudding user response:
It should have no effect on speed, but it affects your ability to perform partial reads. A single object is read as a unit:
If you ask for a single 16384-byte object, and it can only read 16383 bytes,
fread()
fails and you are told nothing was read (the file pointer is still advanced by an unknown amount, though).If you ask for 16384 1-byte objects, and it falls short, you'll know exactly how much you got (which will also tell you how far the file pointer was advanced).
In short, if you want 16384 bytes and anything less is a failure, sure, ask for it as a single unit. But usually, you want to be in a vaguely useful state in the event of a partial read, so you should ask for it as 16384 individual bytes.