Greetings, what i am trying to do is to get as an input a name.wav file and print it's header. I have trouble findining a way to get 4 or 2 bytes from the file in hexadecimal form, if it's little indian change the position of the bytes and then convert it and put it in a struct. An atempt i made that strangely worked is the following. I don't yet understand how it got the right values since some elements were in little indian form. The only problem it has is that audioFormat and numChannels has wrong vallue but other like bloackAlign that are also 2 byte and in little indian form got the right value.
Here is my code:
Code:
//byte = unsigned char
//word = unsigned short int
//dword = unsigned int
typedef struct wavHeader
{ //RIFF CHUNK
byte chunckID[4];
dword chunckSize;
byte format[4];
//FMT SUB-CHUNK
byte subchunk1ID[4];
word subchunk1Size;
word audioFormat;
word numChannels;
dword sampleRate;
dword byteRate;
word blockAlign;
word bitsPerSample;
//DATA SUB-CHUNK
byte subchunk2ID[4];
dword subchunk2Size;
}header;
int checkFileName(char *filename);
void printHeader(header *wav_header);
void list(char **array)
{
header wavHeader;
FILE *pFile;
printf("%s%s\n", "filename: ", array[2]);
if(checkFileName(array[2]) == 0)
{
printf("wrong file name\n");
exit(1);
}
pFile = fopen (array[2] ,"r");
if( pFile != NULL)
{
fread(&wavHeader, sizeof(header), 1, pFile);
fclose(pFile);
printHeader(&wavHeader);
}
else
{
printf("file doesn't exist\n");
exit(1);
}
}