I was answering pset4 when they asked me to reverse a WAV file. I got to work and made a code with seemingly no mistakes at all. Here is the code:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "wav.h"
int check_format(WAVHEADER header);
int get_block_size(WAVHEADER header);
int main(int argc, char *argv[])
{
// Ensure proper usage
if (argc != 3) {
printf("Usage: ./reverse input.wav output.wav\n");
return 1;
}
// Open input file for reading
// TODO #2
char *file = argv[1];
FILE *inptr = fopen(file, "rb");
if (inptr == NULL) {
printf("Could not open\n");
return 1;
}
// Read header
// TODO #3
WAVHEADER header;
fread(&header, sizeof(WAVHEADER), 1, inptr);
// Use check_format to ensure WAV format
// TODO #4
if (check_format(header) == 0) {
printf("Error.");
return 1;
}
if (header.audioFormat != 1) {
printf("Error.");
return 1;
}
// Open output file for writing
// TODO #5
char *outfile = argv[2];
FILE *outptr = fopen(outfile, "wb");
if (outptr == NULL) {
printf("Could not open\n");
return 1;
}
// Write header to file
// TODO #6
fwrite(&header, sizeof(WAVHEADER), 1, outptr);
// Use get_block_size to calculate size of block
// TODO #7
int size = get_block_size(header);
// Write reversed audio to file
// TODO #8
if (fseek(inptr, size, SEEK_END)) {
printf("Error.");
return 1;
}
else {
BYTE buffer[size];
while(ftell(inptr) - size > sizeof(header)) {
if (fseek(inptr, - 2 * size, SEEK_CUR)) {
return 1;
}
fread(buffer, size, 1, inptr);
fwrite(buffer, size, 1, outptr);
}
fclose(outptr);
fclose(inptr);
}
return 0;
}
int check_format(WAVHEADER header)
{
if (header.format[0] == 'W' && header.format[1] == 'A' && header.format[2] == 'V' && header.format[3] == '\0') {
return 1;
}
return 0;
}
int get_block_size(WAVHEADER header)
{
// TODO #7
int size = header.numChannels * header.bitsPerSample / 8;
return 0;
}
but for some reason, to my suprise, when i ran check50, it gave me these results:
:) reverse.c exists
:) reverse.c compiles
:) reverse.c handles lack of input file
:( reverse.c creates an output file
expected exit code 0, not 1
:| reverse.c writes WAV header to output file
can't check until a frown turns upside down
:| reverse.c reverses ascending scale
can't check until a frown turns upside down
i looked at everything in the code, changed some things, looked at many tutorials, but alas, none could get me to a solution.