|
-
Nov 14th, 2002, 07:21 PM
#1
Thread Starter
Fanatic Member
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:
Option Explicit
Dim VersionCheck As Integer
Dim nLen As Integer, nLen2 As Integer
Private Type TITS
Name As String
Age As Byte
Comments As String
End Type
Dim TheAge As Byte
Private Sub Command1_Click()
CommonDialog1.Filter = "T i T S (*.tits)|*.tits"
CommonDialog1.ShowOpen
End Sub
Private Sub Command2_Click()
Dim ageX As Byte
Dim CommentsX As String, NameX As String
If FilesHere(Text1.Text) Then
Open Text1.Text For Binary Access Read Lock Write As #1
Get #1, , VersionCheck
If VersionCheck <> 6 Then MsgBox ("Not made in TFV 6")
Get #1, , nLen
NameX = Space(nLen)
Get #1, , NameX
Get #1, , nLen2
CommentsX = Space(nLen2)
Get #1, , nLen2
Get #1, , CommentsX
Get #1, , ageX
Close #1
Text2.Text = CommentsX
Text4.Text = ageX
Text3.Text = NameX
Else
MsgBox ("File not found, dummy!"), , "Where"
End If
End Sub
Private Sub Command3_Click()
TheAge = Val(Text4.Text)
Dim MyFile() As TITS
'If FilesHere(Text1.Text) Then
' If UCase(Right(Text1.Text, 4)) = "TITS" Then Kill Text1.Text
' End If
Open Text1.Text For Binary Access Write Lock Read Write As #1
Put #1, , VersionCheck
nLen = Len(Text3.Text)
Put #1, , nLen
Put #1, , Text3.Text
nLen2 = Len(Text2.Text)
Put #1, , nLen2
Put #1, , Text2.Text
Put #1, , TheAge
Close #1
End Sub
Private Sub Form_Load()
VersionCheck = 6
Text1.Text = App.Path & "\Example.tits"
End Sub
Private Function FilesHere(TheFileName As String) As Boolean
If FileLen(TheFileName) > 0 Then
FilesHere = True: Exit Function
Else
FilesHere = False: Exit Function
End If
End Function
Last edited by scr0p; Nov 15th, 2002 at 03:28 PM.
asdf
-
Nov 14th, 2002, 07:27 PM
#2
Thread Starter
Fanatic Member
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.
-
Nov 14th, 2002, 08:06 PM
#3
Addicted Member
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!'
-
Nov 14th, 2002, 10:35 PM
#4
Thread Starter
Fanatic Member
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?
-
Nov 15th, 2002, 12:37 AM
#5
Addicted Member
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|