how do I get necessary values of header of ID3v2?
题
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.
解决方案
You should read only once the header. i.e. if you have
struct id3v2hdr {
char TAG[3];
unsigned char version;
unsigned char subversion;
...
}
Your code would be:
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);
}
Note that version
and subversion
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:
fread(&tag, 1, sizeof(tag),file); // tag is structure with elements of header
is unnecessary (tough it would show the intent much more clearly).
其他提示
Are you reading enough bytes? You are passing the address of tag.TAG, but supplying sizeof(tag) and not sizeof(tag.TAG).
That would be %c
for printing a char and not %s
(used for printing null-terminated char*
):
printf("ID3v2.%c.%c", tag.version, tag.subversion);
Use %d
if you want to see the byte as a number.