cmp3id3v2

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.


Solution

  • 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).