nmemb stands for: "number of members". Quite arguably, though ;)
The point is you're reading data from
stream and writing it into
ptr (EDIT: actually you don't write data into
ptr; you write it into the location in memory that is pointed to by
ptr) (a so called "buffer"). But you'll probably get in trouble if the structure that
ptr points to is not big enough to handle all the data retrieved by
So, the designers of
fread built into it a tool for you, as a user, to prevent buffer overflows: you "allow" it to read in
nmemb items, each of size
size (total bytes read = nmemb * size) each time it's invoked.
Such total is the value returned by the function, which you're using to validate your reading process.
Every time you invoke the function, it moves a so-called
cursor (EDIT: the cursor is often referred to as the "file position indicator".) from the actual position (initially, the beginning) in the
stream (for that's what
FILE *file actually is) such total number of bytes ahead. That is, until you decide to
stream, or until the special symbol
EOF ("End Of File") is read, which forces
fread() to return.
According to it's
If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).
Such return value is useful in order to determine when you've reached the end of the file, and need to move on in your algorithm.
One more hint regarding your actual implementation: if you defined
BITMAPFILEHEADER as integer constants, e.g.:
#define BITMAPFILEHEADER 10
#define RGBTRIPLE 20
You're probably going to get into trouble.
Those "#" statements are delt with by the preprocessor ("pp"): they're called "preprocessor directives". What the pp does is, simply, replace every occurence of
RGBTRIPLE with the value
20 before the compilation. Such value is, in fact, a constant integer.
So, when you execute:
What actually gets executed after the pp replacement is:
Since 20 is an
int, you'll get back the size of the
type int as defined in your system (same as what you do with
malloc), so the result is (in most systems):
which is probably not what you expect.
If you're in doubt, try printing out the value of:
and see what comes out ;)