D
David Mathog
When reading a binary input stream with fread() one can
read N bytes in two ways :
count=fread(buffer,1,N,fin); /* N bytes at a time */
or
count=fread(buffer,N,1,fin); /* 1 buffer at a time */
I would assume the latter form would be faster, or at least
less of a load on the CPU. That's just an assumption though,
is it typically true?
Speed matters here, but the form which (I suspect) is faster can't
handle a partial input block. If the input file isn't a multiple
of N it will give an error on the final read, and fread()
provides no possible way of figuring how many, if any of the
data bytes in buffer are valid.
For the program I'm working on at the moment when it's
reading from a tape drive I'm pretty much guaranteed that
the input is a multiple of the block size. If it isn't, then there
was some sort of read error or maybe the wrong block size was
specified. However when it's reading across the network the
input might be some odd size. If it is, it needs to be padded
out to the full block size before it is written to the tape drive.
As far as I can tell the only way to do that is with the first form.
Well, unless I put "dd" with padding turned on in a pipe upstream
from the program, but I'd rather handle this situation internally
within the one program.
Thanks,
David Mathog
read N bytes in two ways :
count=fread(buffer,1,N,fin); /* N bytes at a time */
or
count=fread(buffer,N,1,fin); /* 1 buffer at a time */
I would assume the latter form would be faster, or at least
less of a load on the CPU. That's just an assumption though,
is it typically true?
Speed matters here, but the form which (I suspect) is faster can't
handle a partial input block. If the input file isn't a multiple
of N it will give an error on the final read, and fread()
provides no possible way of figuring how many, if any of the
data bytes in buffer are valid.
For the program I'm working on at the moment when it's
reading from a tape drive I'm pretty much guaranteed that
the input is a multiple of the block size. If it isn't, then there
was some sort of read error or maybe the wrong block size was
specified. However when it's reading across the network the
input might be some odd size. If it is, it needs to be padded
out to the full block size before it is written to the tape drive.
As far as I can tell the only way to do that is with the first form.
Well, unless I put "dd" with padding turned on in a pipe upstream
from the program, but I'd rather handle this situation internally
within the one program.
Thanks,
David Mathog