c - Significance of two arguments in fread? -
when reading documentation fread here, explains 2 arguments after void *ptr multiplied determine amount of bytes being read / written in file. below function header fread given link:
size_t fread(void *ptr, size_t size, size_t nmemb, file *stream); my question is, other returned value function, there behavior or performance difference between calling each of these:
// assume arr int[some_large_number] , fp file* fread(arr, sizeof(arr), 1, fp); fread(arr, sizeof(arr) / sizeof(int), sizeof(int), fp); fread(arr, sizeof(int), sizeof(arr) / sizeof(int), fp); fread(arr, 1, sizeof(arr), fp); and 1 best practice? or more general question is, how decide specify each of arguments in given scenario?
edit
to clarify, not asking justification of 2 arguments instead of one, i'm asking general approach on deciding pass arguments in given scenario. , this answer massimiliano linked in comments , cited provides 2 specific examples , doesn't sufficiently explain why behavior happens.
there behavior difference if there not enough data satisfy request. page linked to:
the total number of elements read returned size_t object, integral data type. if number differs nmemb parameter, either error had occurred or end of file reached.
so if specify there 1 element of size sizeof(arr), , there not enough data fill arr, won't data returned. if do:
fread(arr, sizeof(int), sizeof(arr) / sizeof(int), fp); then arr partially filled if there not enough data.
the third line of code naturally fits api of fread. however, use 1 of other forms if document why not doing normal thing.
Comments
Post a Comment