如何获取 ID3v2 标头的必要值?
我正在尝试读取 mp3 文件的 ID3V2 标头。我可以获取/打印 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.
I'm trying to read header of ID3V2 of mp3 files. I can get/print ID3 and want to print out "version" and "subversion" which is type char, but I can't get what i need.
here is code:
}
.....
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该只阅读一次标题。即如果您有
您的代码将是:
请注意,
version
和subversion
是字节大小的整数,而不是可打印字符,因此您应该使用%hhu
(%hhd
如果它们已签名)作为其格式规范。另外,指向结构体第一个元素的指针和指向结构体的指针比较相等,因此
无需将
fread
行更改为:(尽管它会更清楚地显示意图)。You should read only once the header. i.e. if you have
Your code would be:
Note that
version
andsubversion
are byte-sized integers, not printable characters, so you should use%hhu
(%hhd
if they are signed) as its format specification.Also, the pointer to the first element of a struct, and the pointer to a struct compare equal, so changing your
fread
line to:is unnecessary (tough it would show the intent much more clearly).
您读取了足够的字节吗?您正在传递 tag.TAG 的地址,但提供 sizeof(tag) 而不是 sizeof(tag.TAG)。
Are you reading enough bytes? You are passing the address of tag.TAG, but supplying sizeof(tag) and not sizeof(tag.TAG).
这将是用于打印字符的
%c
而不是%s
(用于打印以 null 结尾的char*
):使用
% d
如果您想将字节视为数字。That would be
%c
for printing a char and not%s
(used for printing null-terminatedchar*
):Use
%d
if you want to see the byte as a number.