Read binary file with numbers and text

Discussion in 'C++' started by greko2009, Dec 5, 2009.

  1. greko2009

    greko2009 New Member

    Joined:
    Dec 5, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    I have a binary file of this format:

    1st 2 bytes = total number of entries (This appears only once at the file beginning)

    Then each record is of the following format:

    2 Bytes = Entry no.
    2 Bytes = Entry category
    2 Bytes = Description length
    n Bytes = Description

    for example if someone opens the file with a hex editor here is what he sees:

    Code:
    02 00 01 00 3C 00 18 00Welcome home dear friend02 00 3C 00 07 00Go Away
    
    I can read the numbers fine using:

    Code:
    
    short int msgnumber;
    short int category;
    short int msglen;
    
    ifstream myfile;
    myfile.open (datfile,ios::in | ios::binary);
    
    while (!myfile.eof() && a < 2)
    {
       myfile.read(reinterpret_cast<char*>( &msgnumber ),sizeof(msgnumber));
       myfile.read(reinterpret_cast<char*>( &category ),sizeof(category));
       myfile.read(reinterpret_cast<char*>( &msglen ),sizeof(msglen));
       
       //Read the text somehow????
    }
    
    but I can't read the text. Can someone please give me some directions?

    Thanks
     
  2. greko2009

    greko2009 New Member

    Joined:
    Dec 5, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Re: Read binary file with numbers and text [Solved!]

    Problem solved!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice