I'm confused as to what exactly is going on in this code

This is a discussion on I'm confused as to what exactly is going on in this code within the C++ Programming forums, part of the General Programming Boards category; Here is some code I found for loading Milkshape3f files (ms3d). When I found it, it was for the most ...

  1. #1
    Shadow12345
    Guest

    I'm confused as to what exactly is going on in this code

    Here is some code I found for loading Milkshape3f files (ms3d).
    When I found it, it was for the most part uncommented, so I added some before posting.
    Code:
    	ifstream inputFile( filename, ios::in | ios::binary | ios::nocreate );
    	if ( inputFile.fail())
    		return false;	// "Couldn't open the model file."
    
    	inputFile.seekg( 0, ios::end );			//GO TO THE END OF THE FILE
    	long fileSize = inputFile.tellg();		//SEE HOW BIG THE FILE IS
    	inputFile.seekg( 0, ios::beg );			//POSITION BACK TO THE BEGINNING SO YOU CAN READ EVERYTHING IN
    
    	byte *pBuffer = new byte[fileSize];		//CREATE A BUFFER TO HOLD EVERYTHING IN THE FILE
    	inputFile.read( pBuffer, fileSize );	//READ EVERYTHING INTO PBUFFER UNTIL FILESIZE IS REACHED
    	inputFile.close();
    
    	const byte *pPtr = pBuffer;				
    	MS3DHeader *pHeader = ( MS3DHeader* )pPtr;	
    	
    	//pPTR CONTAINS EVERYTHING IN THE MS3D FILE
    	pPtr += sizeof( MS3DHeader );				
    
    	if ( strncmp( pHeader->m_ID, "MS3D000000", 10 ) != 0 )
    		return false; // "Not a valid Milkshape3D model file."
    So basically the buffer contains everything that was stored in the ms3d file. Everything in the ms3d file is read into pBuffer until the filesize is reached (the end of the file).

    pPtr is assigned to everything in pBuffer. pHeader (which is a milkshape 3d header) is assigned to pPtr. the sizeof a milkshape 3d header is added to pPtr. Then information in the header is checked to make sure it is a valid ms3d file. The thing I dont' get is why is the sizeof a header added to pPtr (pPtr is everything in pBuffer, pBuffer is everything that is in the file). Shouldn't the header information already be in the file without having to modify any of the buffers?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Not quite sure what you mean. pHeader points to the beginning of the buffer (the header) and pPtr points to the rest of the buffer (after the header, thus adding sizeof(MS3DHeader) to the pointer).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Whats this | ?

    I am curoius shouldnt this

    ( filename, ios::in | ios::binary | ios::nocreate );

    be this

    (filename, ios::in || ios::binary || ios::nocreate)


    I am curious because I never seen just '|' before. Can anyone explain this to me, plz?



    I am curious...


    -"God must like crazy people!"
    I am one of his crazy people!

    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    The single | is used when you are combining bits. Each of the ios bits are refenced in these names and you combine them together using the single |. The || is used as an or in an if (or similar) statement.
    Best Regards,

    Bonkey

  5. #5
    Shadow12345
    Guest
    I don't see why the size of a MS3DHeader must be added to pPtr, unless it is just some way of keeping track what you have done with the file.

    throughout the entire model loading routine the size of various data types are added to pPtr.
    Last edited by Shadow12345; 11-11-2002 at 10:30 AM.

  6. #6
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    Originally posted by bonkey
    The single | is used when you are combining bits. Each of the ios bits are refenced in these names and you combine them together using the single |. The || is used as an or in an if (or similar) statement.
    Thanks BONKEY for the lesson. Cool, learned something different.
    Thnxs!
    cj
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Yes. You add the sizeof() to skip over the header and have pPtr point to the rest of the file, that way pHeader points to the header, and pPtr points to the data.
    pPtr->pBuffer
    [Header] // Equal to sizeof(MS3DHeader)
    [Rest of File]

    pBuffer
    pHeader->[Header]
    pPtr->[Rest of File]

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Shadow12345
    I don't see why the size of a MS3DHeader must be added to pPtr, unless it is just some way of keeping track what you have done with the file.

    throughout the entire model loading routine the size of various data types are added to pPtr.
    pPtr points to the data you're currently interested in, and since pHeader points to the header all the time you can skip it with pPtr.

    Try this program and you'll see how it works:
    Code:
    #include <iostream.h>
    
    #define Loop(n) for(int i=0; i<n; i++)
    #define NrOfIntegers 6
    
    int main()
    {
       int Data[NrOfIntegers] = {6, 5, 4, 3, 2, 1};
       int* Pointer = Data;
    
       //Loop 6 times
       Loop(NrOfIntegers)
       {
          //Print the integers in the array
          cout << *Pointer << endl;
          Pointer++;
       }
    
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Shadow12345
    Guest
    Yes, actually, what you both (Eibro and Magos) said makes sense. That program did help too, i just imagined pointer was pPtr and data was the file. The higher the value of pointer the further into the file it will look. I think it clicked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. Replies: 4
    Last Post: 01-15-2002, 11:04 PM
  5. I am confused with this code that somebody gave me
    By face_master in forum C++ Programming
    Replies: 14
    Last Post: 11-16-2001, 12:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21