I'm having problems with the Lab4 exercise. I've coded this for the TODO functions:

// TODO: Copy header from input file to output file

    HEADER header;

    while (fread(&header, sizeof(header), 1, input))

            fwrite(&header, sizeof(header), 1, output);

// TODO: Read samples from input file and write updated data to output file

     BUFFER buffer;
     NEW_BUFFER new_buffer;

        while (fread(&buffer, sizeof(buffer), 1, input))

              new_buffer = buffer*factor;
              fwrite(&new_buffer, sizeof(new_buffer), 1, output);

    // Close files

When I apply "check50" I get the following feedback:

Results for cs50/labs/2021/x/volume generated by check50 v3.2.2
:) volume.c exists
:) volume.c compiles
:( reduces audio volume, factor of 0.5 correctly
    audio is not correctly altered, factor of 0.5
:( reduces audio volume, factor of 0.1 correctly
    audio is not correctly altered, factor of 0.1
:( increases audio volume, factor of 2 correctly
    audio is not correctly altered, factor of 2

I really don't understand what I'm doing wrong or missing... If anyone sees the problem, would you please give a hand to understand it?

Thank you so much!



The issue is that you are reading your header in a loop. This action only needs to be done once, which is why the exact size of the header is provided. By reading it in a loop, you are reading through the entire input file in chunks the size of header and writing them to the output file. Then, you are performing the requisite modifications to the buffer and writing it to the same file (producing a filer with the audio at its original speed followed by the modified speed). This is providing a file that does no conform to the specifications. Removing the operations on the header contents from a loop should fix this for you.

Hope this helps. If it does, feel free to click the check mark next to this answer. But if not, let me know and I will try to help further.

  • Thank you so much! I removed the loop for HEADER and it worked perfectly ok!!! Apr 3 '21 at 21:11

This code snippet does not show what HEADER, BUFFER, or NEW_BUFFER are. Any one of these could be the source of the problem. If the size of BUFFER doesn't match the sample size called for in the lab, the multiplication could behave unexpectedly.

You must log in to answer this question.

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