Easy pointer question - passing

I have some simple code to read in some information from an mp3 and spit it out.  Problem is, it doesn't work.  I haven't worked with C or C++ for well over a year, and I'm *quite* rusty on pointers.  I know exactly what's wrong with the code and why it's bombing (it always spits out mpeg 2.5 and default in printLayer cause *header is 255), I just don't know how to read the data I'm looking for.  You'll see what I'm trying to do when you see the code.  If you test this and it says it can't find a frame header, use another mp3.  I was using 2 bytes for the sake of simplicity, and some mp3s' frame headers are on odd boundries.

Here ya go:

#define HEADERBYTES 2

void printType (unsigned char* header)
{
      switch (*header & 0x1800) // 00000000000110000000000000000000
      {
      case 00:
            cout << "MPEG Version 2.5";
            break;
      case 10:
            cout << "MPEG Version 2";
            break;
      case 11:
            cout << "MPEG Version 1";
            break;
      default:
            cout << "Something weird happened...";
      }
}

void printLayer (unsigned char* header)
{
      switch (*header & 0x6000) // 00000000000001100000000000000000
      {
      case 01:
            cout << "MPEG Layer 3";
            break;
      case 10:
            cout << "MPEG Layer 2";
            break;
      case 11:
            cout << "MPEG Layer 1";
            break;
      default:
            cout << "More weirdness...";
      }
}

int getFileSize (string &sFilename)
{
      struct _stat statBuffer;

      if (_stat(sFilename.c_str(), &statBuffer) != 0)
      {
            cout << "can't get file size" << endl;
            return 0;
      }

      return statBuffer.st_size;
}

int findFrame (ifstream &in)
{
      int iBytesRead = 0;
      unsigned char *mp3Header = new unsigned char[HEADERBYTES];
      int iFileSize = getFileSize(string("test.mp3"));
      
      for (; (iBytesRead + HEADERBYTES) < iFileSize; iBytesRead += HEADERBYTES)
      {
            in.read((char *)mp3Header, HEADERBYTES);

            if ((mp3Header[0] == 0xFF) && ((mp3Header[1] & 0xE0) == 0xE0))
            {
                  cout << "found header at " << iBytesRead << endl;
                  return iBytesRead;
            }
            in.seekg(iBytesRead + HEADERBYTES);
      }
      return -1;
}

int main(int argc, char *argv[])
{
      ifstream in;
      int iFirstFramePos = 0;

      in.open("test.mp3");

      if (!in)
      {
            cout << "Can't open test.mp3" << endl;
            return 0;
      }
      
      int iPos = findFrame(in);

      if (iPos == -1)
      {
            cout << "Couldn't find first frame header!" << endl;
            return 0;
      }

      unsigned char *pFrameHeader = new unsigned char[4];
      in.seekg(iPos);
      in.read((char *)pFrameHeader, 4);

      printType(pFrameHeader);
      printLayer(pFrameHeader);
      return 0;
}

If you're not sure what I'm trying to get at, http://pipin.tmd.ns.ac.yu/extra/fileformat/modules/mp3/mpeghdr.htm will clear it all up.  How can I pass my frame as a contiguous stream of bits?  If the bits were on even numbered boundaries I wouldn't be having this problem, but all solutions I've come up with so far work, but are total hacks.

Thanks for any help!
LVL 1
dbunderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jhshuklaCommented:
found at least one error. below is correct version:
void printType (unsigned char* header)
{
     switch (header[1] & 0x18) // 00000000 00011000 00000000 00000000
     {
     case 0x00:
          cout << "MPEG Version 2.5";
          break;
     case 0x10:
          cout << "MPEG Version 2";
          break;
     case 0x18:
          cout << "MPEG Version 1";
          break;
     default:
          cout << "Something weird happened...";
     }
}
==================================
other thing to keep in mind is that there might be multiple possible occurences of FrameSync signature although all of them are not really framesyncs. a good way to tell whether the signature you found is an actual sync is to go just past the end of the frame and find a sync again. else, go back to the place where you stopped reading (i.e. EEEEFFGH byte) and start reading from there (EEEEFFGH included in new reading).
btw i am wondering why are you reading 2 bytes at a time? shouldn't you be reading one byte at a time? what if the second byte of mp3Header is actually the first byte and you discarded it because the preceding byte was not 0xFF?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dbunderAuthor Commented:
After I posted the message I went in and wrote almost the same code.  This is just test code, so I'm not worrying about too much.  Was using 2 bytes for the sake of simplicity (most frame headers are on even numbered bytes in my experience) but that will change.  And I'll be doing more robust frame header checking in the future, yes.

Let's just consider this closed, since I figured it out and wrote more or less the same code as you :)
0
jhshuklaCommented:
no prob
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.