Results 1 to 14 of 14

Thread: Open a binary file.

  1. #1
    Join Date
    Mar 2010
    Posts
    38
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Open a binary file.

    I want to open binary file using Qt. I dave a description of file:
    Qt Code:
    1. =================================================================
    2. IFP file format for Grand Theft Auto III and Vice City animation
    3. Written by Hollower (hollower@hotmail.com)
    4. =================================================================
    5. (updated Jan 2004)
    6.  
    7. This file is intended for programmers only.
    8.  
    9. First column is the number of bytes. V means that the length varies.
    10. Second column is the suggested data type (C/C++).
    11. Third column is a brief description of the data.
    12. Strings are padded to 4-byte alignment.
    13.  
    14. This is how I broke up the file for reading in. The section markers
    15. indicate the more "correct" way, but I used this simpler grouping
    16. (ie. why worry about the parent section of "INFO" blocks when they
    17. should always be in this order anyway), and I used the section markers
    18. for error checking. Using my grouping also makes it easier to output
    19. an IFP file that is valid for GTA3/GTA:VC. This may change in future
    20. GTA games.
    21.  
    22.  
    23. IFP FILE HEADER
    24. ----------------
    25. 4 (char[4]) "ANPK" --- section marker
    26. 4 (int) Offset to end of file
    27. 4 (char[4]) "INFO" --- section marker
    28. 4 (int) Offset to end of header
    29. 4 (int) Number of anims
    30. V (char[V]) Internal file name (null terminated string)
    31.  
    32. ANIMATION INFO
    33. ----------------
    34. 4 (char[4]) "NAME" --- section marker
    35. 4 (int) Length of anim name string
    36. V (char[V]) Anim name (null terminated string)
    37. 4 (char[4]) "DGAN" --- section marker
    38. 4 (int) Offset to end of animation
    39. 4 (char[4]) "INFO" --- section marker
    40. 4 (int) Offset to first object (usually 8)
    41. 4 (int) Number of objects
    42. 4 (int) NULL
    43.  
    44. OBJECT INFO
    45. ----------------
    46. 4 (char[4]) "CPAN" --- section marker
    47. 4 (int) Offset to end of object
    48. 4 (char[4]) "ANIM" --- section marker
    49. 4 (int) Offset to frame info (usually 48)
    50. 28 (char[28]) Object name (null terminated string, padded to 28 bytes)
    51. 4 (int) Number of frames
    52. 4 (int) NULL
    53. 4 (int) Index of last frame (Number of frames - 1)
    54. 4 (int) Index of next sibling (-1 if none)
    55. 4 (int) Index of previous sibling (-1 if none)
    56.  
    57. FRAME INFO
    58. ----------------
    59. 4 (char[4]) Frame type (see below)
    60. 4 (int) Length of frame data
    61.  
    62. FRAME DATA
    63. ----------------
    64. Frame data is all floats. Structure depends on frame type.
    65.  
    66. Frame types:
    67.  
    68. "KRT0" Root quaternion rotation (float x,y,z,w),
    69. vector translation (float x,y,z),
    70. time (float seconds)
    71.  
    72. "KR00" Child rotation, time
    73.  
    74. "KRTS" Scaled rotation, translation, scale (float x,y,z), time
    75.  
    76.  
    77. Root frames contain extra data for translating the model in the
    78. world. Child frames are simply rotations, concatenated as they
    79. go up the hierarchy.
    80.  
    81. You can probably guess, the letters here stand for
    82. (K)eyframe, ®otation, (T)ranslation, (S)cale, (0)None
    83. so presumably there could also be other combinations like "K0T0" but
    84. the above are the only ones found in all of the files I studied.
    85.  
    86.  
    87. Notes
    88. ----------------
    89.  
    90. Overall file structure looks like this:
    91.  
    92. Header
    93. |
    94. +-Animation
    95. | |
    96. | +-Object
    97. | | |
    98. | | +-Frame
    99. | | +-Frame
    100. | | +-Frame
    101. | | +- ...
    102. | |
    103. | +-Object
    104. | | |
    105. | | +-Frame
    106. | | +-Frame
    107. | | +-Frame
    108. | | +- ...
    109. | |
    110. | +- ...
    111. |
    112. +-Animation
    113. | |
    114. | +-Object
    115. | | |
    116.  
    117. ...and so on.
    118.  
    119. Object names are a reference to the DFF mesh or bone
    120. which will be rotated with the frame data. The string
    121. is not case sensitive.
    122.  
    123. An animation does not need to reference every part of a
    124. model, only the ones that will move. They also do not
    125. require an equal number of frames or matching time index.
    126.  
    127. There are sometimes anomalies like anims with 0 objects
    128. or objects with 0 frames.
    129.  
    130. For reference, the base pose can be viewed by creating
    131. animations with Rotation = {0.0f, 0.0f, 0.0f, 1.0f}.
    132.  
    133.  
    134.  
    135. I hope this file has been helpful. Now let's see some editors!
    136.  
    137. - Hollower
    To copy to clipboard, switch view to plain text mode 

    Ok, how to open it??

    My code:
    Qt Code:
    1. QString str = QFileDialog::getOpenFileName(0, "Open IFP", "", "*.ifp");
    2. QFile file(str);
    3. if(file.open(QIODevice::ReadOnly)) {
    4. QDataStream stream(&file);
    5. qDebug() << stream;
    To copy to clipboard, switch view to plain text mode 

    What i must do next??

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Open a binary file.

    Surely you can't use QDataStream.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2010
    Posts
    38
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Open a binary file.

    But what should i use?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Open a binary file.

    I'd probably use 'map', but you could use 'read', since your reading the file.

  5. #5
    Join Date
    Mar 2010
    Posts
    38
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Open a binary file.

    So, what command i must use? Can anyone writean example?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Open a binary file.

    Quote Originally Posted by TJSonic View Post
    So, what command i must use?
    QFile::read(), as already said.

    Can anyone writean example?
    Read the docs for QFile.

    Small print: Actually we do know what you are asking for and we're just playing stupid until you are sure if you really want us to write a parser for your file structure (you don't want us to do it, I assure you).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2010
    Posts
    38
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Open a binary file.

    Ok, can you tel me, how for example read firs section of file and convert in to QString?

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Open a binary file.

    I think I'd prefer to use a QByteArray rather than a QString, considering the binary nature of the file, and you can see from the documentation how to read into a QByteArray.

    You can then even convert the tags, such as 'ANPK' to quint32 and compare them that way instread, which is properly what the original designer intended, or cast to an appropriate struct.

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Open a binary file.

    Quote Originally Posted by TJSonic View Post
    Ok, can you tel me, how for example read firs section of file and convert in to QString?
    I could! ...but I wont.

    Read the suggested part of the documentation and your description file and put 1 and 1 together.

    (If you ask more detailed question, we will help further.)


    Edit: too late.

  10. #10
    Join Date
    Mar 2010
    Posts
    38
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Open a binary file.

    Qt Code:
    1. void MainWindow::on_action_IFP_triggered()
    2. {
    3. QString str = QFileDialog::getOpenFileName(0, "Open IFP", "", "*.ifp");
    4. QFile file(str);
    5. // QFile::map()
    6. if(file.open(QIODevice::ReadOnly)) {
    7. QByteArray line = file.readLine();
    8. qDebug() << line;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    I recived "ANPKin"
    I do this right?

  11. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Open a binary file.

    You do realise what readLine does, right?

    The hint is in the name.

  12. #12
    Join Date
    Mar 2010
    Posts
    38
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Open a binary file.

    As i anderstand it reads fil line and goes no next, right? In this case why next line shows "NA" when in hex we have

    00000000:41 4e 50 4b ec f1 15 00 49 4e 46 4f 0b 00 00 00 ANPKмс..INFO....
    00000010:02 00 00 00 63 6e 74 5f 31 61 00 dc 4e 41 4d 45 ....cnt_1a.ЬNAME
    00000020:08 00 00 00 43 53 70 68 69 6c 00 44 47 41 4e ....CSphil.DGAN

    Qt Code:
    1. QByteArray line = file.readLine();
    2. QString line1 = file.readLine();
    3. QByteArray line2 = file.readLine();
    4. QByteArray line3 = file.readLine();
    5. QByteArray line4 = file.readLine();
    6. QByteArray line5 = file.readLine();
    7. // process_line(line);
    8. qDebug() << line << line1;
    9. qDebug() << line1;
    10. qDebug() << line2;
    11. qDebug() << line3;
    12. qDebug() << line4;
    13. qDebug() << line4;
    To copy to clipboard, switch view to plain text mode 

    I recive :
    "ANPKin
    "NA"
    "A???f?¶eI?o®NADjuA?u?????°?x=lK????f ?I??·NAKruAAµ??"""?^a~=»?¤?5??¬TI? EANAzuA`I??«?*???=A????_?°uH?WCNA??uA˜ ?C?333?? ?=µ?????? H?CNA?uA?&E??»;?*9~=3U??cq??8H?"ANA\?uA
    "?AUuA?E??DDA??O?=?'??3$?+µB?#oIAjOuAXe????E ?µ??=©0???[$?¤?B?uOIAoOuA`??III?*7?=?O??rE$?=B??IAIuA ??N?C??=?c˜?qs%?)?A?p?IA§EuA U??UUO???=?u??u-&??'A?r|IAA?uA?G????U? e~=Ak???¬&?u?@?Y`IA??uAi???YY?Zx=
    ???E2'?]h@?ACIAg±uAaL??""a?0q=(???e'??a??m*IAI¤uA? 8??ff??¤ j=?Q??Pz(?Ev??:IA??uA`??«?e??ve=o%??e*(?WX??. uIA¦?uAoE??iii?oeb=}????(?R??}aIA?uA?'??33o? ?d=n????(??<???IIAa?uAP°??ww??µRk='R???A(? )7??Z»IA??uAF»??»u?v?n=/4??`A(?o,??y©IAE©uA0?»?
    "@®._=?1|??)????nIAo?uA0?»?II @A?]=? {?/?(?oB??
    bIAGCuApe??ii@? a=LA?©?(?A~???[IA?OuA »?@?¶c=z???eb(?????VIA1UuAAC»?33@AAa =??Au'?(y??vKIANYuAe??UU@?o^=? ??W?'??h@?CIA??uAa}??ww@??]=j?L'?;?@?UBIA;auA(?????@Xg\=M?}?WQ'?E?@?IIIA} auAo>???»@µ9[=iH|?iZ'?±?@?
    OIA0auA?»???Y@*Z=Y{?4O'??¤@? TIA#auAE???
    "@®._=?1|??)????nIAo?uA0?»?II @A?]=? {?/?(?oB??
    bIAGCuApe??ii@? a=LA?©?(?A~???[IA?OuA »?@?¶c=z???eb(?????VIA1UuAAC»?33@AAa =??Au'?(y??vKIANYuAe??UU@?o^=? ??W?'??h@?CIA??uAa}??ww@??]=j?L'?;?@?UBIA;auA(?????@Xg\=M?}?WQ'?E?@?IIIA} auAo>???»@µ9[=iH|?iZ'?±?@?
    OIA0auA?»???Y@*Z=Y{?4O'??¤@? TIA#auAE???

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Open a binary file.

    Hint: binary files have no "lines", so readLine() will stop reading at a random place (or exactly when it enounters a \n character).

    I suggest you take a piece of paper and write down a concept of how you intend to process the file step by step (the specification in the first post is all help you need). Only then sit down in front of your computer and start implementing it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Open a binary file.

    Like I got told on the first day of my employment induction "Projects are typically 80% documentation and research, and 20% coding." and it's true. There's no point randomly coding stuff and hoping it'll work. Figure it all out first then code it.

Similar Threads

  1. Read binary file
    By jaca in forum Qt Programming
    Replies: 9
    Last Post: 28th March 2012, 08:38
  2. Open a file with affiliated binary
    By bunjee in forum Newbie
    Replies: 1
    Last Post: 23rd August 2009, 06:17
  3. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 3rd April 2009, 23:50
  4. cannot execute binary file
    By mgturner in forum Installation and Deployment
    Replies: 1
    Last Post: 16th March 2009, 17:04
  5. How to Print a doc file (or binary file) to printer
    By rmagro in forum Qt Programming
    Replies: 15
    Last Post: 5th September 2008, 15:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.