0
#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
    // TODO #1 (done)
    if (argc != 3) // 3 arguments 1 for the run, 1 for input , 1 for output
    {
        printf("./reverse input.wav output.wav\n");
        return 1;
    }


    // Open input file for reading
    // TODO #2 (done)
    FILE *file; // creating a pointer to file
    file = fopen(argv[1], "r");//opening the file with "r" for read only

    if (file == NULL) // making sure the file is opened
    {
        return 1;
    }


    // Read header
    // TODO #3(done)
    WAVHEADER header; //creating a place to store the header
    WAVHEADER* header1 = &header; // creating a pointer for the sake of the function w/ size of the header
    fread(header1, sizeof(WAVHEADER), 1, file); //reading from the file into the header
    //(the file to read into, size of what we will read, n. of times, source)
    header = *header1; //putting the data we took in it's place


    // Use check_format to ensure WAV format
    // TODO #4(done)
    if(check_format(header) != 1)
    {
        return 1;
    }


    // Open output file for writing
    // TODO #5 (done)
    FILE* outfile;
    outfile = fopen(argv[2], "w"); //"w" for write
    if (outfile == NULL)
    {
        return 1;
        printf("output file failed to open \n");
    }


    // Write header to file
    // TODO #6 (done)
    fwrite(header1, sizeof(WAVHEADER), 1, outfile);
    //(the sorce, size, n. of times, the file to write in)


    // Use get_block_size to calculate size of block
    // TODO #7(done)
    int size = get_block_size(header);


    // Write reversed audio to file
    // TODO #8
    int pointerStart = ftell(file);
    fseek(file, 0, SEEK_END);
    fseek(outfile, pointerStart, SEEK_SET);
    int pointerEnd = ftell(file);
    char buffer[size];
    for (int i =0 ; i < pointerEnd ; i++)
    {
        fseek(file, -i, SEEK_END);
        fread(buffer, size,1, file);
        fseek(outfile, pointerStart +i, SEEK_SET);
        fwrite(buffer, size, 1, outfile);
    }

    //closing files
    fclose(file);
    fclose(outfile);
}

int check_format(WAVHEADER header)
{
    // TODO #4 (done)
    BYTE test[4] = "WAVE";
    for (int i = 0; i < 4; i++)
    {
        if (header.format[i] != test[i])
        {
            return 2;
        }
    }
    return 1;
}

int get_block_size(WAVHEADER header)
{
    // TODO #7 (done)
    int bsize;
    bsize = header.numChannels * (header.bitsPerSample/8);
    return bsize;
}

this is the whole code and i can't find what's wrong i tried changing the

for (int i =0 ; i < pointerEnd ; i++)
    {
        fseek(file, -i, SEEK_END);
        fread(buffer, size,1, file);
        fseek(outfile, pointerStart +i, SEEK_SET);
        fwrite(buffer, size, 1, outfile);
    }

to

for (int i =0 ; i < pointerEnd - pointerStart; i++)
    {
        fseek(file, -i, SEEK_END);
        fread(buffer, size,1, file);
        fseek(outfile, pointerStart +i, SEEK_SET);
        fwrite(buffer, size, 1, outfile);

because it made sense for a moment but it didn't work so i removed it

1 Answer 1

1

Fixed it

    int headsize = 44;
    int pointerStart = ftell(file);
    fseek(file, 0, SEEK_END);
    fseek(outfile, headsize, SEEK_SET);
    int pointerEnd = ftell(file);
    BYTE buffer[size];
    fseek(file, -1*size, SEEK_END);
    for (int i= (pointerEnd - pointerStart)/size ; i>0 ; i--)
    {
        fread(buffer, size,1, file);
        fwrite(buffer, size, 1, outfile);
        fseek(file, -2*size, SEEK_CUR);
    }

first i changed the buffer into BYTE instead of char since the data is in bytes and i set the size to size(block_size) to store one block then i set the file pointer to one block before the end of file to start with it then in the loop i set i to the number of blocks by getting the number of bytes and deviding it by the size of a single block and i started reading one block into the buffer then writing it into the outfile then i made the pointer of the file move two blocks back, one for the block it just copied and one for the next block since it moves when it reades anything

You must log in to answer this question.

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