C,Wav文件块大小问题

时间:2015-04-08 02:43:30

标签: c wav chunks

鉴于WAV文件,我想打印出ChunkSize,SubChunk1Size和SubChunk2Size。

enter image description here

以下是我写的代码:

#include <stdio.h>
#include <stdlib.h>

struct wavFile{

    char ChunkID[4]; //"RIFF", GOOD
    unsigned int ChunkSize;
    char Format[4]; //"WAVE", GOOD
    char Subchunk1ID[4]; //"fmt", GOOD
    unsigned int Subchunk1Size;  //GOOD
    unsigned short int AudioFormat; //GOOD
    unsigned short int NumChannels; //GOOD
    unsigned int SampleRate; //GOOD
    unsigned int ByteRate;
    unsigned int BlockAlign;
    unsigned int BitsPerSample;
    char SubChunk2ID[4]; //"data", prints weird symbols instead of "data"
    unsigned int Subchunk2Size;

};

int main()
{
    struct wavFile w;
    int headerSize = sizeof(w);
    FILE *fp;

    fp = fopen(wavFilePathGoesHere, "r");
    fread(&w, headerSize, 1, fp);



    printf("ChunkSize: %d, SubChunk1Size: %d, SubChunk2Size: %d\n", w.ChunkSize, w.Subchunk1Size, w.Subchunk2Size);
    //SubChunk2Size: -76154081

    fclose(fp);
    return 0;
}

我在结构中注释的变量为&#34; GOOD&#34;实际上给出了正确的值,所以这些都是好的。 printf语句给出负SubChunk2Size值(-76154081)。当然,这可能是对的。我不知道我在这里做错了什么。

2 个答案:

答案 0 :(得分:2)

使用%u而不是%d来获取无符号整数的printf。具体见http://www.cplusplus.com/reference/cstdio/printf/

specifier   Output                   Example
d or i      Signed decimal integer   392
u           Unsigned decimal integer 7235

答案 1 :(得分:2)

BlockAlignBitsPerSample应该是短片。