I'm doing CS50 (Week 4 Lab 4 Volume) and I have to copy a header from an input WAV file to output file (the 44 first bytes).
To start, I created the array uint8_t header[HEADER_SIZE];. Then, I used fread. But why the right solution is : fread(header, HEADER_SIZE, 1, input); and not something like : fread(header, sizeof(uint8_t), HEADER_SIZE, input);?
Don't it mean that all the header will be copied at once in the header array in a single element (which will lead to an error since an element must have 1 byte due to its type uint8_t)? Does the compiler now how to handle the situation when reading at once many bytes into an array with only 1 byte per element?
Thank you!
HEADER_SIZEand the number of elements is1. The difference is that on success,freadwith return1andHEADER_SIZErespectively.HEADER_SIZEorHEADER_SIZEitems of size 1. What's the difference in the end? Besides of course, return value offreadwill be different.sizeof(char)is always 1 (by definition). So, using it in calculations is deprecated. A little tricky withsizeof(uint8_t)but don't use for the same reason. Strictly speaking,fread(header,sizeof(header),1,input)might be the best, but ... Inglibc[linux], thesizeandnmembare multiplied together as the first line offread:size_t bytes_requested = size * count;and that is used internally. So, it's a bit of a moot point.read(the syscall) uses a byte count, so I do:fread(header,1,sizeof(header),input)to get a byte count return value