3

I am puzzled by the lab volume in week 4. The few questions I have in particular are:

  1. How does the code below know to skip the first 44 bytes of header when it begins multiplying the bytes by factor?

  2. In the while loop, what is the condition or increment that allows the loop to run properly. For example, earlier we would say while x < y x++. Where is the x++ equivalent here, why will the loop run differently on the second and third pass than it does in the first pass.

  3. Lastly, I absolutely can't wrap my mind around the input arguments in fread and fwrite even after going over the manual documentation. What do they mean? Any help welcome.

// TODO: Copy header from input file to output file
    uint8_t header[HEADER_SIZE];

    fread(header, HEADER_SIZE, 1, input);

    fwrite(header, HEADER_SIZE, 1, output);
    // TODO: Read samples from input file and write updated data to output file
    int16_t buffer;
    while (fread(&buffer, sizeof(int16_t), 1, input))
    {
        buffer *= factor;
                  fwrite(&buffer, sizeof(int16_t), 1, output);
    }

2 Answers 2

3

3rd question: fread takes as arguments the buffer or variable (array) you want to read into, it has that "&" because arrays are kind of pointers, then it takes how many bytes to read, then how many times to read it, and where does it read that from, the same goes for fwrite with changing the input with the output as it looks in the code you posted. fread just reads from the file and fwrite writes into it, I don't know if this helps since I basically re-said the documentation. The 2nd one : so this one is actually easier when you get it, fread actually returns how much of the file is left to read (I had to google the source code to confirm it, it was return (to_read - bytes) / size. when it has read all the size you wanted read, the bytes and to_read are the same, meaning it'll return a zero 0. Which considered the boolean for false in C, so it stops reading and breaks out of the while loop. 1st one : Since fread doesn't have a loop starting condition which confused you earlier, so it just finishes from where it left off, meaning it will automatically skip the header that it already finished.

3
  • Thank you for explaining. I am still confused about the 1st question. The fread function is called for the header file which is at the beginning of the file, the next time it is called separately for the audio samples but it somehow knows to skip the first 44 bytes i dont get how that happens. Commented Jan 2, 2022 at 13:10
  • Everytime the fread is called within the while loop, isn't it still called separately after all since you are running it over and over again? It felt weird for me too but the fact that I was fine with it in the while loop even though is running for a whole new time made it feel normal, it just carries on where it left off in the file wherever you left off
    – Chakib37
    Commented Jan 3, 2022 at 9:44
  • I am not sure since I didn't try it but I think closing the file is the only thing that will get it to reinitialize, I'm not sure how it works really though I could be wrong.
    – Chakib37
    Commented Jan 3, 2022 at 9:45
0

To answer your first question, the first freads move the file pointer to the end of the header and the second freads picks up the file pointer from that point. Reference: https://stackoverflow.com/questions/10696845/does-fread-move-the-file-pointer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .