So my homework instructs me to read in a .WAV file and fill out the header and data according to their respective endians. I believe I have malloced enough memory for my header and data struct, but when I run the program, I end up with a Segmentation Fault 11. I can't seem to find where I went wrong. Any help would be appreciated!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "prog9.h"
/*
* little_endian_2 - reads 2 bytes of little endian and reorganizes it into big endian
* INPUTS: fptr - pointer to the wav file
* OUTPUTS: none
* RETURNS: the data that is converted to big endian
*/
int little_endian_2(FILE *fptr)
{
int count;
char temp[2];
count = 0;
while (count < 2)
{
fscanf (fptr, "%s", temp);
count++;
}
char holder;
holder = temp[1];
temp[1] = temp[0];
temp[0] = holder;
printf ("%c", temp[0]);
printf("%c", temp[1]);
count = atoi(temp);
return count;
}
/*
* little_endian_4 - reads 4 bytes of little endian and reorganizes it into big endian
* INPUTS: fptr - pointer to the wav file
* OUTPUTS: none
* RETURNS: the data that is converted to big endian
*/
int little_endian_4(FILE *fptr)
{
int count = 0;
char temp[4];
while (count < 4)
{
fscanf (fptr, "%c", temp);
count++;
}
char holder;
holder = temp[3];
temp[3] = temp[0];
temp[0] = holder;
holder = temp[2];
temp[2] = temp[1];
temp[1] = holder;
count = atoi(temp);
return count;
}
/*
* read_file - read the wav file and fill out the wav file struct
* INPUTS: wavfile - a string that contains the name of the file
* OUTPUTS: none
* RETURNS: the pointer to the wav file struct created in this function
* SIDE EFFECT: prints the information stored in the wav struct
*/
WAV *read_file(char *wavfile)
{
WAV* wav_ptr = (WAV*)malloc(sizeof(WAV));
FILE *fp;
fp = fopen(wavfile,"r");
fscanf (fp, "%c", wav_ptr->RIFF); //For RIFF
wav_ptr->ChunkSize = little_endian_4(fp); //For ChunkSize
fscanf (fp, "%c", wav_ptr->WAVE); //For WAVE
fscanf (fp, "%c", wav_ptr->fmt); //For fmt
wav_ptr->Subchunk1Size = little_endian_4(fp); //for Subchunk1size
wav_ptr->AudioFormat = little_endian_2(fp); //For audioformat
wav_ptr->NumChan = little_endian_2(fp); //for numchan
wav_ptr->SamplesPerSec = little_endian_4(fp); //for samp/sec
wav_ptr->bytesPerSec = little_endian_4(fp); //for bytes/sec
wav_ptr->blockAlign = little_endian_2(fp); //for block alight
wav_ptr->bitsPerSample = little_endian_2(fp); //for bits/sample
wav_ptr->extra = (char*)malloc((wav_ptr->Subchunk1Size - 16)*sizeof(char)); //allocate memory for extra
fscanf (fp, "%c", wav_ptr->extra); //if extra
fscanf (fp, "%c", wav_ptr->Subchunk2ID); // for subchunk2id
wav_ptr->Subchunk2Size = little_endian_4(fp); //for subchunk 2
wav_ptr->data = (short int*)malloc((wav_ptr->Subchunk2Size)*sizeof(short int)); //allocate memory for data
fscanf (fp, "%hd", wav_ptr->data); //for data
return wav_ptr;