Results 1 to 5 of 5

Thread: My own file format, Need help[soLVEd]

  1. #1

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720

    My own file format, Need help[soLVEd]

    I am trying to make a file format so I can better my self at binary file access. The extension is *.tits and it holds a name, age and comments. This is the code:

    I want to check if the file exists, the function I made isnt working, "FilesHere", Can someone tell me whats wrong with it.

    Problems:

    When I save the file, it makes example.tits but if I open it in notepad I can see the comments and name, I know its because they are string but is there anyway to hide that.

    When loading, I get the name back but age is 0 and I get like half of the comments.


    VB Code:
    1. Option Explicit
    2. Dim VersionCheck As Integer
    3. Dim nLen As Integer, nLen2 As Integer
    4. Private Type TITS
    5.     Name As String
    6.     Age As Byte
    7.     Comments As String
    8. End Type
    9. Dim TheAge As Byte
    10.  
    11. Private Sub Command1_Click()
    12. CommonDialog1.Filter = "T i T S (*.tits)|*.tits"
    13. CommonDialog1.ShowOpen
    14. End Sub
    15.  
    16. Private Sub Command2_Click()
    17. Dim ageX As Byte
    18. Dim CommentsX As String, NameX As String
    19.     If FilesHere(Text1.Text) Then
    20.         Open Text1.Text For Binary Access Read Lock Write As #1
    21.             Get #1, , VersionCheck
    22.             If VersionCheck <> 6 Then MsgBox ("Not made in TFV 6")
    23.             Get #1, , nLen
    24.             NameX = Space(nLen)
    25.             Get #1, , NameX
    26.             Get #1, , nLen2
    27.              CommentsX = Space(nLen2)
    28.             Get #1, , nLen2
    29.             Get #1, , CommentsX
    30.             Get #1, , ageX
    31.         Close #1
    32.         Text2.Text = CommentsX
    33.         Text4.Text = ageX
    34.         Text3.Text = NameX
    35.     Else
    36.         MsgBox ("File not found, dummy!"), , "Where"
    37.     End If
    38.    
    39. End Sub
    40.  
    41. Private Sub Command3_Click()
    42. TheAge = Val(Text4.Text)
    43.  
    44. Dim MyFile() As TITS
    45.     'If FilesHere(Text1.Text) Then
    46.     '    If UCase(Right(Text1.Text, 4)) = "TITS" Then Kill Text1.Text
    47.    ' End If
    48.         Open Text1.Text For Binary Access Write Lock Read Write As #1
    49.             Put #1, , VersionCheck
    50.             nLen = Len(Text3.Text)
    51.             Put #1, , nLen
    52.             Put #1, , Text3.Text
    53.             nLen2 = Len(Text2.Text)
    54.             Put #1, , nLen2
    55.             Put #1, , Text2.Text
    56.             Put #1, , TheAge
    57.         Close #1
    58. End Sub
    59.  
    60. Private Sub Form_Load()
    61. VersionCheck = 6
    62.     Text1.Text = App.Path & "\Example.tits"
    63. End Sub
    64.  
    65. Private Function FilesHere(TheFileName As String) As Boolean
    66.     If FileLen(TheFileName) > 0 Then
    67.         FilesHere = True: Exit Function
    68.     Else
    69.         FilesHere = False: Exit Function
    70.     End If
    71. End Function
    Last edited by scr0p; Nov 15th, 2002 at 03:28 PM.
    asdf

  2. #2

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    I think I fixed it. I changed byte to integer, (Can I use LONG instead?) and I took out an extra line for reading comments that was there by accident.
    asdf

  3. #3
    Addicted Member
    Join Date
    Oct 2002
    Location
    Somewhere out in space
    Posts
    151
    you can use any data type you want in your file. The data type is just a way of knowing how much byte you put in the file. What is important is if you put a long at position 300 in your file, to get this value back, you must get a long at position 300, if you use a shorter variable, you will miss some data and if you use a bigger variable, you will get to much byte and the result won't seem accurate.
    'You keep creatures in cages and release them to fight? That's sick!'

  4. #4

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    How do I put it at position 300? If I am just storing lets say an age, can I use a byte? If I am storing the size of a file in bytes, can I use long? etc, and do I just change dim XXX as byte to dim XXX as long. Also If I make a file with strings, does the length HAVE to be right before the string itself?
    asdf

  5. #5
    Addicted Member
    Join Date
    Oct 2002
    Location
    Somewhere out in space
    Posts
    151
    300 was just an exemple, i don't know if you can put data at a precise point in a file if data as not been put before that part. But you can get to position 300 (if there is a value) by using the seek method
    VB Code:
    1. Seek #NumFile,,300
    This would move the file pointer to... well 300

    If I am just storing lets say an age, can I use a byte?
    Well... unless your storing the age of something that can be older than 255, of course you can. But, if you put the age in your file using a byte, you must read the age from the file using a byte variable. That's what i've tried to explain in my previous post.
    If I am storing the size of a file in bytes, can I use long?
    you can use any data type that will be able to hold the numeric value of the file size. The numeric value will represent the file size in byte... you don't need to use a byte variable just because you saving value that represent a number of bytes.

    Also If I make a file with strings, does the length HAVE to be right before the string itself?

    If you save a string to a binary file, you should save the len of the string also so you can size a string variable correctly to get the right amount of data from the file when you try to read your string. (what you already seem to do from the look of your code)

    and for a closing gift : http://rookscape.com/vbgaming/tutS.php this should help you alot to understand how binary files work.

    - Good luck
    - Valkan
    'You keep creatures in cages and release them to fight? That's sick!'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width