0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
andrey
  • 671
  • 1
  • 9
  • 20
  • why do you remove the formatting? –  Jun 16 '11 at 18:51
  • Why are you reading `sizeof(tag)` bytes? the fread call should probably look like `fread(&member, sizeof member, 1, file)`. **And test the return value of `fread()`!** – pmg Jun 16 '11 at 18:52
  • Stop editing answers to reply to them; edit or leave a comment on your own question instead (and if/when you hit 50 rep you can leave comments on the answers as well). – ildjarn Jun 16 '11 at 19:32

3 Answers3

0

Are you reading enough bytes? You are passing the address of tag.TAG, but supplying sizeof(tag) and not sizeof(tag.TAG).

Vinicius Kamakura
  • 7,665
  • 1
  • 29
  • 43
0

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.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
0

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

ninjalj
  • 42,493
  • 9
  • 106
  • 148