给定一个 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)。当然,这不可能是正确的。我不知道我在这里做错了什么。
最佳答案
使用 %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
关于C、Wav 文件 block 大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29504990/