Reading a MP3 header

This is a discussion on Reading a MP3 header within the C Programming forums, part of the General Programming Boards category; First, if you don't know how a mp3 file header is you should google a bit before trying to understand ...

Well, the problem... The first frame header isn't found first. Only after the first iteration, the second time, or not. I think it has something to do with the tag skipping. I get the wrong bitrate, and If I try to read several frames from the same file I get diferent bitrates every time.

Opps... another mistake... It's corrected now, but still doesn't work... now the problem is to make the MP3HEADER structure fit in 4 bytes... #pragma pack directive doesn't work. The compiler gives the framesync field 2 bytes and 3 to the rest of the fields.

Your only portable solution is to read the information into an array of bytes, then extract the information using normal bitwise masking and shifting operations, much like you do in unpacktagsize().

Bitfields are useless for external data types.
For example, given
struct { int layer : 2 ; }
You don't know whether that maps to bits 0,1; 7,8; 14,15; or 30,31. The compiler is free to choose any of those.
Add to that all the different ways of saying "#pragma pack(1)", and likely endian issues across different machines.
All in all, it's just much easier to just do the work yourself.