Home > front end >  A Possible error in solution video of pset4 lab 'volume' within fread() function of header
A Possible error in solution video of pset4 lab 'volume' within fread() function of header

Time:09-22

This is the code from what Brian explains from cs50 week4 lab4

// Modifies the volume of an audio file
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

// Number of bytes in .wav header
const int HEADER_SIZE = 44;

int main(int argc, char *argv[])
{
    // Check command-line arguments
    if (argc != 4)
    {
        printf("Usage: ./volume input.wav output.wav factor\n");
        return 1;
    }

    // Open files and determine scaling factor
    FILE *input = fopen(argv[1], "r");
    if (input == NULL)
    {
        printf("Could not open file.\n");
        return 1;
    }

    FILE *output = fopen(argv[2], "w");
    if (output == NULL)
    {
        printf("Could not open file.\n");
        return 1;
    }

    float factor = atof(argv[3]);

    // 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);
    }
    // Close files
    fclose(input);
    fclose(output);
}

I am getting confused as to what fread() and fwrite() does. It says:

 while(fread(header, HEADER_SIZE, 1, input))

should not it be :

while(fread(header,sizeof(uint8_t), HEADER_SIZE, input)) 

since the syntax is :

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

ptr, which is the address (of the first byte) of memory into which to read the data,

size, which is the size (in bytes) of the type of data to read,

nmemb, which is the number of those types to read at once, and

stream, which is the pointer to a FILE returned by fopen.

And why are we using the address of buffer in fwrite() and fread() and not for header in fwrite() and fread()? Is the value of buffer gonna be overwritten after each loop?

CodePudding user response:

It depends.

If you want to receive one complete header and then process it as a header, with the meaning of the parts of a header, then you should ask for one copy of the kind of header you want to process:

fread(header, HEADER_SIZE, 1, input)

If you want to receive a number of bytes (which happens to be the size of a header) and then process them as separate bytes (i.e. ignore the fact that together they make a header), then you should ask for many bytes:

fread(header,sizeof(uint8_t), HEADER_SIZE, input)

(With explicit permission I add the contribution by WeatherVance. It adds details on the technical consequences to my approach of trying to explain the semantic meaning.)

fread(header, 1, 44, input) and fread(header, 44, 1, input) will both attempt to read up to 44 bytes.
If only 2 bytes could be read then fread will return 2 in the first case and 0 in the second case. Because the first is trying to read 44 items of size 1 and the second wants to read 1 item of size 44.

  • Related