I'm having a difficult time trying to open a WAV file for reading. When I compile and run my code, I do not get any errors. I am adding the code that I am messing with (assuming necessary libraries have been called). The program is supposed to display the contents of the WAV file and although I enter a valid filename and extension, the statement "Invalid Filename. Try Again." is still being printed to the screen. The other method I attempted was enter the directory of the file instead of just the name, and when I do that, my program ends and nothing is displayed. Any guidance would be helpful and thank you in advance!
main(){
FILE *fin;
printf("\nEnter filename of WAV file: \n");
char filename[256];
scanf("%s",&filename);
fin = fopen(filename,"rb"); // opens in rb
if(!fin) // if file doesn't exist
{
printf("Invalid filename. Try again.\n");
}
else // if fin opens succesfully
{
printf("\nFile opened succesfully\n");
char *header;
header = (char *)malloc(44);
if(header == NULL)
{
printf("Error in allocating memory.");
return 0;
}
fread(header,1,44,fin);
char *chunkid;
unsigned int *chunksize;
char *format;
char *subchunk1id;
unsigned int *subchunk1size;
unsigned short int *audioformat;
unsigned short int *numchannels;
unsigned int *samplerate;
unsigned int *byterate;
unsigned short int *blockalign;
unsigned short int *bitspersample;
char *subchunk2id;
unsigned int *subchunk2size;
unsigned int *data;
chunkid = header;
chunksize = (unsigned int *)(header + 4);
format = header + 8;
subchunk1id = header + 12;
subchunk1size = (unsigned int *)(header + 16);
audioformat = (unsigned short int *)(header + 18);
numchannels = (unsigned short int*)(header + 20);
samplerate = (unsigned int*)(header + 24);
byterate = (unsigned int*)(header + 28);
blockalign = (unsigned short int*)(header + 30);
bitspersample = (unsigned short int*)(header + 32);
subchunk2id = header + 36;
subchunk2size = (unsigned int*)(header + 40);
data = (unsigned int*)(header + 44);
printf("\n%c%c%c%c",*(header),*(header+1),*(header+2),*(header+3));
printf("\n%d",*chunksize);
printf("\n%c%c%c%c",*(header + 8),*(header + 9), *(header + 10), *(header + 11));
printf("\n%c%c%c%c",*(header + 12),*(header + 13), *(header + 14), *(header + 15));
printf("\n%d",*subchunk1size);
printf("\n%d",*audioformat);
printf("\n%d",*numchannels);
printf("\n%d",*samplerate);
printf("\n%d",*byterate);
printf("\n%d",*blockalign);
printf("\n%d",*bitspersample);
printf("\n%c%c%c%c",*(header + 36),*(header + 37),*(header + 38), *(header + 39));
printf("\n%d",*subchunk2size);
printf("\n%d",*data);
fclose(fin);
} // end of else
} // end of main