I'm trying to read the header information of a .wav file and I'm kind of stuck.

Basically, the header information is located at different offsets in the file. So for example:

(0-4) = ChunkID
(4-8) = ChunkSize
(8-12) = Format
.. ..
.. ..
.. ..

From this: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

Now the problem I'm having is when I try to read the data like this, the data does not store correctly and I end up with missing data. This is the code I have written:

bool Wav::readHeader(FILE *dataIn)
    // RIFF
    int i=0;

    fread(this->chunkId, sizeof(char), 4, dataIn);
    i += 4;

    fread(&this->chunkSize, sizeof(int), 1, dataIn);

    fread(this->format, sizeof(char), 4, dataIn);

    cout << "RIFF DATA" << endl;

    cout << "Chunk ID: " << this->chunkId << endl;
    cout << "Chunk Size: " << this->chunkSize << endl;
    cout << "Format: " << this->format << endl;

    fread(this->formatId, sizeof(char), 4, dataIn);
    fread(&this->formatSize, sizeof(int), 4, dataIn);
    fread(&this->format2, sizeof(short), 2, dataIn);
    fread(&this->numChannels, sizeof(int), 2, dataIn);
    fread(&this->sampleRate, sizeof(int), 4, dataIn);

    cout << endl << endl;
    cout << "FORMAT DATA" << endl;
    cout << "Format ID: " << this->formatId << endl;
    cout << "Format S: " << this->formatSize << endl;
    cout << "Format SS: " << this->format2 << endl;
    cout << "Channels: " << this->numChannels << endl;
    cout << "Sample Rt: " << this->sampleRate << endl;
    return true;

Here are the class members/variables:

// Define the class-variables for the Header Information
           // riff
           char chunkId[4];
           int chunkSize;
           char format[4];

           // Format
           char formatId[4];
           int formatSize;
           short format2;
           short numChannels;
           int sampleRate;
           int byteRate;
           short align;
           short bitsPerSample;

.. Up to now, this doesn't work and displays:

Chunk ID: RIFF?
Chunk Size: 42776
Format: WAVE

Format ID: fmt 
Format S: 18
Format SS: 1
Channels: 0
Sample Rt: -1494941696

Is there a way that I could possibly read the data from the text file like.. (0-4), (4-8) OR store the first 44 values (unformatted) inside a vector and then convert the data into the header data?

I'm confused to which approach to take.

Hope someone can help me,

Thanks :)

I suppose this will not help, but there is a built in method in windows.h header. You may google its definition. A simple code goes as follows

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.