C、Wav 文件块大小问题

C, Wav File Chunk Sizes Issue

给定一个 WAV 文件,我想打印出 ChunkSize、SubChunk1Size 和 SubChunk2Size。

下面是我写的代码:

#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;
}

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

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

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

BlockAlignBitsPerSample 应该是短裤。