Click here to Skip to main content
15,878,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "wav.h"

int check_format(WAVHEADER header);
int get_block_size(WAVHEADER header);

int main(int argc, char *argv[])
{
    FILE * input;
    FILE * output;
    // Ensure proper usage
    // TODO #1
    if(argc!=3)
    {
        printf("Usage: ./reverse input.wav output.wav\n");
        return 1;
    }

    if(strcmp(argv[1],"input.wav")!=0)
    {
        printf("Input is not a WAV file.\n");
        return 0 ;
    }

if(strcmp(argv[2],"output.wav")==0)
    {
        return 0 ;
    }
    // Open input file for reading
    // TODO #2
   input = fopen("argv[1]","r");
   if(input == NULL)
   {
    printf("Could not open input file.\n");
    return 1;
   }


    // Read header into an array
    // TODO #3
    WAVHEADER header;
fread(&header,sizeof(header),1,input);

    // Use check_format to ensure WAV format
    // TODO #4
check_format(header);
    // Open output file for writing
    // TODO #5
    output =fopen("argv[2]","w");



    // Write header to file
    // TODO #6
fwrite(&header,sizeof(header),1,output);
    // Use get_block_size to calculate size of block
    // TODO #7
    get_block_size(header);

    // Write reversed audio to file
    // TODO #8



    fclose(input);
    fclose(output);
}

int check_format(WAVHEADER header)
{
    char W,A,V,E;
  W=A=V=E=0;
  if(header.format[0]==W && header.format[1]==A && header.format[2]==V && header.format[3]==E)
  {
    return 0;
  }
  else{
    return 1;
  }

}

int get_block_size(WAVHEADER header)
{
int blocksize;
int bytessum;
int channelnum;
channelnum = header.numChannels;
bytessum = header.bitsPerSample/8;
blocksize = bytessum * channelnum;
return blocksize;
}


What I have tried:

This is a code which runs as following ./reverse input.wav output.wav where input is already present,and it is reversed,I know i didnt finish my code but I test the part i made ,and the checker keeps saying output file(output.wav) is not created,so what is my problem.
Posted
Updated 5-Jan-23 5:49am
Comments
jeron1 5-Jan-23 11:41am    
Is your input file opening?

input = fopen("argv[1]","r"); // <== was is input after this line?
0x01AA 5-Jan-23 11:51am    
I would say: if(strcmp(argv[2],"output.wav")!=0) instead of if(strcmp(argv[2],"output.wav")==0)

Why are you doing this:
C++
if(strcmp(argv[1],"input.wav")!=0)
{
    printf("Input is not a WAV file.\n");
    return 0 ;
}

If you are going to use fixed filenames then there is no point in allowing the user to enter them at the console.

And later you have:
C++
if(strcmp(argv[2],"output.wav")==0)
    {
        return 0 ;
    }

So, if argv[2] is equal to "output.wav", the program terminates immediately.

And even later you have:
C++
input = fopen("argv[1]","r");

So you are trying to open a file named "argv[1]", rather than the file whose name is stored in the variable argv[1].

And the same issue at:
C++
output =fopen("argv[2]","w");


All in all, quite a few basic errors to be corrected.
 
Share this answer
 
Comments
CPallini 5-Jan-23 12:32pm    
5.
First off, remember that it will be created in the current directory (i.e. the application exe directory) unless you specify a full path. C library function - fwrite()[^]

A "checker" application may be checking for a valid sound file, not just it's actual existence! I'd start by copying the whole input file to the output rather than just a header.

I'd also note that the fwrite function returns the number of bytes written - so it would be worth checking that against what you expected as well: use the debugger to find out while your code is running!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900