What is the difference between:
fread( buf, sizeof buf, 1, file ); // 'file' is valid open 'FILE *'
and
fread( buf, 1, sizeof buf, file );
Parameters 2 and 3 are size_t size, size_t nmemb
described as "fread() reads nmemb items of data, each size bytes long". I think the final number of bytes read must be size * nmemb
but for some reason only second syntax (with size=1) is working for me.
The difference is in the following two:
nmemb
). This is different in your two statement, if they succeed.In the first case:
fread( buf, sizeof(buf), 1, file )
You will get all or nothing; fread
cannot read something that is smaller than sizeof(buf)
in case file is smaller.
In the second case:
fread( buf, 1, sizeof(buf), file );
You allow fread
to read as much bytes as possible, even if less than sizeof(buf)
are available in the file. The number of elements (bytes in this case) actually read will be returned.