0

can anybody tell me what is wrong with this code block. When i use check50 it fails all three objectives.

h_buffer: header buffer s_buffer: sample buffer

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

uint8_t h_buffer[HEADER_SIZE];
fread(h_buffer,sizeof(uint8_t),HEADER_SIZE,input);
fwrite(h_buffer,sizeof(uint8_t),HEADER_SIZE,output);

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

1 Answer 1

1

From man fread

On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).

fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.

When the end of file is reached, fread returns 0. But don't start researching feof quite yet, you don't need it for this problem. You should be able to change the while conditional based on the above information and have success.

This program gets stuck in an infinite loop. It is a good idea to have a reasonable expectation of success before checking the code with check50.

You must log in to answer this question.

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