gpt4 book ai didi

c - 如何获取 ID3v2 header 的必要值?

转载 作者:行者123 更新时间:2023-11-30 18:05:29 27 4
gpt4 key购买 nike

我正在尝试读取 mp3 文件的 ID3V2 header 。我可以获取/打印 ID3,并想打印出 char 类型的“version”和“subversion”,但我无法得到我需要的内容。

这里是代码:

    }
.....
fseek(file,0,SEEK_SET);
fread(&tag.TAG, 1, sizeof(tag),file); // tag is structure with elements of header

if(strncmp(tag.TAG,"ID3", 3) == 0)
{
fread(&tag.version,1, sizeof(tag),file);
fread(&tag.subversion,1, sizeof(tag),file);

printf("ID3v2.%s.%s", tag.version, tag.subversion);
}
}

A.

最佳答案

您应该只阅读一次标题。即如果你有

struct id3v2hdr {
char TAG[3];
unsigned char version;
unsigned char subversion;
...
}

您的代码将是:

fseek(file,0,SEEK_SET); 
fread(&tag.TAG, 1, sizeof(tag),file); // tag is structure with elements of header

if(strncmp(tag.TAG,"ID3", 3) == 0)
{
printf("ID3v2.%hhd.%hhd", tag.version, tag.subversion);
}

请注意,versionsubversion 是字节大小的整数,而不是可打印字符,因此您应该使用 %hhu (% hhd(如果它们已签名)作为其格式规范。

此外,指向结构体第一个元素的指针和指向结构体的指针比较相等,因此将 fread 行更改为:

fread(&tag, 1, sizeof(tag),file); // tag is structure with elements of header

是不必要的(尽管它会更清楚地显示意图)。

关于c - 如何获取 ID3v2 header 的必要值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6376980/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com