• Welcome to Valhalla Legends Archive.
 

Binary file reading problems....

Started by Tontow, June 10, 2005, 09:32 PM

Previous topic - Next topic

Tontow

I have decided to use a binary file to store the settings for my bot.

  It seems to write the file correctly, but it dosent read the data correctly (it reads the first few varables correctly and then it starts reading incorrectly)
(note: I can provide the form for download if needed)

Private Type ccolor
    Red As String
    Green As String
    Blue As String
End Type
Private Type logininfo
    Value As String
End Type



'colors
Dim cjoin As ccolor
Dim cleave As ccolor
Dim crecivewhisper As ccolor
Dim cisay As ccolor
Dim cbotcommands As ccolor
Dim cchannel As ccolor
Dim csentwhisper As ccolor
Dim cchannisfull As ccolor
Dim cchannnotexist As ccolor
Dim cchannrestrected As ccolor
Dim cinfo As ccolor
Dim cerror As ccolor
Dim cemote As ccolor
Dim cloggedonas As ccolor
Dim cserver As ccolor
Dim cbuttons As ccolor
Dim cbuttontext As ccolor
Dim cchatback As ccolor
Dim cuserlistback As ccolor
Dim cuserlisttext As ccolor
Dim csendboxback As ccolor
Dim csendboxtext As ccolor

'login info
Dim username As logininfo
Dim userpass As logininfo
Dim scbwkey As logininfo
Dim scbwpath As logininfo
Dim port As logininfo
Dim connecttype As logininfo

Private Sub btnsetconnectopt_Click(index As Integer)
txtsetting(27).Text = index
End Sub


Private Sub btnsetOK_Click()

Dim tempstring() As String

Dim filepath As String
filepath = App.Path & "\setting.ini"
   
   


Set fs = CreateObject("Scripting.FileSystemObject")
fs.DeleteFile filepath




'open file for random access
Open filepath For Binary As #50

'colors
tempstring = Split(txtsetting(0).Text, ",")
cjoin.Red = tempstring(0)
cjoin.Green = tempstring(1)
cjoin.Blue = tempstring(2)
Put #50, , cjoin


tempstring = Split(txtsetting(1).Text, ",")
cleave.Red = tempstring(0)
cleave.Green = tempstring(1)
cleave.Blue = tempstring(2)
Put #50, , cleave

tempstring = Split(txtsetting(2).Text, ",")
crecivewhisper.Red = tempstring(0)
crecivewhisper.Green = tempstring(1)
crecivewhisper.Blue = tempstring(2)
Put #50, , crecivewhisper

tempstring = Split(txtsetting(3).Text, ",")
cisay.Red = tempstring(0)
cisay.Green = tempstring(1)
cisay.Blue = tempstring(2)
Put #50, , cisay

tempstring = Split(txtsetting(4).Text, ",")
cbotcommands.Red = tempstring(0)
cbotcommands.Green = tempstring(1)
cbotcommands.Blue = tempstring(2)
Put #50, , cbotcommands

tempstring = Split(txtsetting(5).Text, ",")
cchannel.Red = tempstring(0)
cchannel.Green = tempstring(1)
cchannel.Blue = tempstring(2)
Put #50, , cchannel

tempstring = Split(txtsetting(6).Text, ",")
csentwhisper.Red = tempstring(0)
csentwhisper.Green = tempstring(1)
csentwhisper.Blue = tempstring(2)
Put #50, , csentwhisper

tempstring = Split(txtsetting(7).Text, ",")
cchannisfull.Red = tempstring(0)
cchannisfull.Green = tempstring(1)
cchannisfull.Blue = tempstring(2)
Put #50, , cchannisfull

tempstring = Split(txtsetting(8).Text, ",")
cchannnotexist.Red = tempstring(0)
cchannnotexist.Green = tempstring(1)
cchannnotexist.Blue = tempstring(2)
Put #50, , cchannotexist

tempstring = Split(txtsetting(9).Text, ",")
cchannrestrected.Red = tempstring(0)
cchannrestrected.Green = tempstring(1)
cchannrestrected.Blue = tempstring(2)
Put #50, , cchannrestrected

tempstring = Split(txtsetting(10).Text, ",")
cinfo.Red = tempstring(0)
cinfo.Green = tempstring(1)
cinfo.Blue = tempstring(2)
Put #50, , cinfo

tempstring = Split(txtsetting(11).Text, ",")
cerror.Red = tempstring(0)
cerror.Green = tempstring(1)
cerror.Blue = tempstring(2)
Put #50, , Error

tempstring = Split(txtsetting(12).Text, ",")
cemote.Red = tempstring(0)
cemote.Green = tempstring(1)
cemote.Blue = tempstring(2)
Put #50, , cemote

tempstring = Split(txtsetting(13).Text, ",")
cloggedonas.Red = tempstring(0)
cloggedonas.Green = tempstring(1)
cloggedonas.Blue = tempstring(2)
Put #50, , cloggedonas

tempstring = Split(txtsetting(14).Text, ",")
cserver.Red = tempstring(0)
cserver.Green = tempstring(1)
cserver.Blue = tempstring(2)
Put #50, , cserver

tempstring = Split(txtsetting(15).Text, ",")
cbuttons.Red = tempstring(0)
cbuttons.Green = tempstring(1)
cbuttons.Blue = tempstring(2)
Put #50, , cbuttons

tempstring = Split(txtsetting(16).Text, ",")
cbuttontext.Red = tempstring(0)
cbuttontext.Green = tempstring(1)
cbuttontext.Blue = tempstring(2)
Put #50, , cbuttontext

tempstring = Split(txtsetting(17).Text, ",")
cchatback.Red = tempstring(0)
cchatback.Green = tempstring(1)
cchatback.Blue = tempstring(2)
Put #50, , cchatback

tempstring = Split(txtsetting(18).Text, ",")
cuserlistback.Red = tempstring(0)
cuserlistback.Green = tempstring(1)
cuserlistback.Blue = tempstring(2)
Put #50, , cuserlistback

tempstring = Split(txtsetting(19).Text, ",")
cuserlisttext.Red = tempstring(0)
cuserlisttext.Green = tempstring(1)
cuserlisttext.Blue = tempstring(2)
Put #50, , cuserlisttext

tempstring = Split(txtsetting(20).Text, ",")
csendboxback.Red = tempstring(0)
csendboxback.Green = tempstring(1)
csendboxback.Blue = tempstring(2)
Put #50, , csendboxback

tempstring = Split(txtsetting(21).Text, ",")
csendboxtext.Red = tempstring(0)
csendboxtext.Green = tempstring(1)
csendboxtext.Blue = tempstring(2)
Put #50, , csendboxtext


'longin info
username.Value = txtsetting(22).Text
Put #50, , username

userpass.Value = txtsetting(23).Text
Put #50, , userpass

scbwkey.Value = txtsetting(24).Text
Put #50, , scbwkey

scbwpath.Value = txtsetting(25).Text
Put #50, , scbwpath

port.Value = txtsetting(26).Text
Put #50, , port

connecttype.Value = txtsetting(27).Text
Put #50, , connecttype

Close #50

End Sub

Private Sub Form_load()

Open App.Path & "\setting.ini" For Binary As #50
Get #50, 1, cjoin
Get #50, Seek(50), cleave
Get #50, Seek(50), crecivewhisper
Get #50, Seek(50), cisay
Get #50, Seek(50), cbotcommands
Get #50, Seek(50), cchannel
Get #50, Seek(50), csentwhisper
Get #50, Seek(50), cchannisfull
Get #50, Seek(50), cchannnotexist
Get #50, Seek(50), cchannrestrected
Get #50, Seek(50), cinfo
Get #50, Seek(50), cerror
Get #50, Seek(50), cemote
Get #50, Seek(50), cloggedonas
Get #50, Seek(50), cserver
Get #50, Seek(50), cbuttons
Get #50, Seek(50), cbuttontext
Get #50, Seek(50), cchatback
Get #50, Seek(50), cuserlistback
Get #50, Seek(50), cuserlisttext
Get #50, Seek(50), csendboxback
Get #50, Seek(50), csendboxtext
Get #50, Seek(50), username
Get #50, Seek(50), userpass
Get #50, Seek(50), scbwkey
Get #50, Seek(50), scbwpath
Get #50, Seek(50), port
Get #50, Seek(50), connecttype
Close #50

txtsetting(0).Text = cjoin.Red & "," & cjoin.Green & "," & cjoin.Blue

txtsetting(1).Text = cleave.Red & "," & cleave.Green & "," & cleave.Blue
txtsetting(2).Text = crecivewhisper.Red & "," & crecivewhisper.Green & "," & crecivewhisper.Blue
txtsetting(3).Text = cisay.Red & "," & cisay.Green & "," & cisay.Blue
txtsetting(4).Text = cbotcommands.Red & "," & cbotcommands.Green & "," & cbotcommands.Blue
txtsetting(5).Text = cchannel.Red & "," & cchannel.Green & "," & cchannel.Blue
txtsetting(6).Text = csentwhisper.Red & "," & csentwhisper.Green & "," & csentwhisper.Blue
txtsetting(7).Text = cchannisfull.Red & "," & cchannisfull.Green & "," & cchannisfull.Blue
txtsetting(8).Text = cchannnotexist.Red & "," & cchannnotexist.Green & "," & cchannnotexist.Blue
txtsetting(9).Text = cchannrestrected.Red & "," & cchannrestrected.Green & "," & cchannrestrected.Blue
txtsetting(10).Text = cinfo.Red & "," & cinfo.Green & "," & cinfo.Blue
txtsetting(11).Text = cerror.Red & "," & cerror.Green & "," & cerror.Blue
txtsetting(12).Text = cemote.Red & "," & cemote.Green & "," & cemote.Blue
txtsetting(13).Text = cloggedonas.Red & "," & cloggedonas.Green & "," & cloggedonas.Blue
txtsetting(14).Text = cserver.Red & "," & cserver.Green & "," & cserver.Blue
txtsetting(15).Text = cbuttons.Red & "," & cbuttons.Green & "," & cbuttons.Blue
txtsetting(16).Text = cbuttontext.Red & "," & cbuttontext.Green & "," & cbuttontext.Blue
txtsetting(17).Text = cchatback.Red & "," & cchatback.Green & "," & cchatback.Blue
txtsetting(18).Text = cuserlistback.Red & "," & cuserlistback.Green & "," & cuserlistback.Blue
txtsetting(19).Text = cuserlisttext.Red & "," & cuserlisttext.Green & "," & cuserlisttext.Blue
txtsetting(20).Text = csendboxback.Red & "," & csendboxback.Green & "," & csendboxback.Blue
txtsetting(21).Text = csendboxtext.Red & "," & csendboxtext.Green & "," & csendboxtext.Blue

txtsetting(22).Text = username.Value
txtsetting(23).Text = userpass.Value
txtsetting(24).Text = scbwkey.Value
txtsetting(25).Text = scbwpath.Value
txtsetting(26).Text = port.Value
txtsetting(27).Text = connecttype.Value
End Sub

Yegg

Just a quick note, instead of having multiple
tempstring = Split(txtsetting(2).Text, ",")
crecivewhisper.Red = tempstring(0)
crecivewhisper.Green = tempstring(1)
crecivewhisper.Blue = tempstring(2)
Put #50, , crecivewhisper

paragraphs of code, you can use a for loop and only use one.

Warrior

..and how is a for loop not worse?
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

Tontow

Quote from: Yegg on June 10, 2005, 09:36 PM
Just a quick note, instead of having multiple
tempstring = Split(txtsetting(2).Text, ",")
crecivewhisper.Red = tempstring(0)
crecivewhisper.Green = tempstring(1)
crecivewhisper.Blue = tempstring(2)
Put #50, , crecivewhisper

paragraphs of code, you can use a for loop and only use one.

Are you saying that I would be better off haveing my user defined varable be something like

Private Type setting
    Red() As String
    Green() As String
    Blue() As String
    Value() As String
End Type

and then looping through? (useing an array to store the data)

OnlyMeat

You could simplify that code by filling structures with the settings you wish to write, then just write the entire structure to disk. You could then read the entire structure back out from the file in one read operation :)

Of course there are structure member alignment issues. But provided your application is the only one that reads them you should be fine. Portabillity isn't an issue with vb so that rules the other member alignment issues out.

It will reduce your code size massively and also decrease the processor cycles required for read/write operations significantly.

An example will go something like this :-



type MyType
setting2 as integer
setting3 as integer
setting4 as integer
setting5 as integer
setting6 as integer
end type

dim t as MyType
' Fill in settings
' t.setting1 = ...

open "c:\TestFile.bin" for binary as #1

put , ,t

close #1



Note i haven't tested this code, it's just an example.

Dyndrilliac

#5
First of all, that's the ugliest code I've ever seen.

Second of all, why are you using constant integers for the instance of the open file?
Quote from: Tontow on June 10, 2005, 09:32 PM
'open file for random access
Open filepath For Binary As #50

That's just begging for an access violation. Do this:FileInstance = FreeFile
Open Filepath For Binary As #FileInstance
Edit: FileInstance is an Integer, by the way.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

R.a.B.B.i.T

Quote from: Tontow on June 10, 2005, 09:32 PM
I have decided to use a binary file to store the settings for my bot.

  It seems to write the file correctly, but it dosent read the data correctly (it reads the first few varables correctly and then it starts reading incorrectly)
(note: I can provide the form for download if needed)

Private Type ccolor
    Red As String
    Green As String
    Blue As String
End Type
Private Type logininfo
    Value As String
End Type

You have to allocate memory to strings if you're using Binary file access.

Tontow

#7
I thought random access was fixed and binary wasn't?????

(edit: ya, random is fixed, binary isn't fixed.  So if im useing binary why do I have to allocate space to it?)

OnlyMeat

Quote from: Tontow on June 11, 2005, 10:46 AM
I thought random access was fixed and binary wasn't?????

(edit: ya, random is fixed, binary isn't fixed.  So if im useing binary why do I have to allocate space to it?)

It allows you to read/write structures of a fixed size. If this is not done you can't directly read/write structures with strings in them.

R.a.B.B.i.T

Quote from: Tontow on June 11, 2005, 10:46 AM
I thought random access was fixed and binary wasn't?????

(edit: ya, random is fixed, binary isn't fixed.  So if im useing binary why do I have to allocate space to it?)
Also note that declaring the variable only creates the pointer.  Space is only allocated after a value is passed to the variable, otherwise it is a NP.

Stealth

Quote from: rabbit on June 11, 2005, 03:38 PM
Quote from: Tontow on June 11, 2005, 10:46 AM
I thought random access was fixed and binary wasn't?????

(edit: ya, random is fixed, binary isn't fixed.  So if im useing binary why do I have to allocate space to it?)
Also note that declaring the variable only creates the pointer.  Space is only allocated after a value is passed to the variable, otherwise it is a NP.

I'm pretty sure declaring a variable or public type of a fixed size in VB will actually dimension the space in memory and fill it with nulls..
- Stealth
Author of StealthBot

Hdx

#11
Dude ever heard of the nifty function RGB()?

Also I hate prinintg types to files so here:

Private Type CColor
    Blue As Long
    Green As Long
    Red As Long
End Type
Private X As Integer, strData() As String, lngBuff As Long, _
    intFreeFile As Integer, strFile As String, strBuff As String, _
    ccColors(0 To 21) As CColor, strOthers(0 To 5) As String

Private Function Conv(lngIn As String) As CColor
    Dim strBuff As String, ccOut As CColor
    strBuff = Right$("000000" & lngIn, 6)
    With ccOut
        .Blue = CLng("&H" & Mid$(strBuff, 1, 2))
        .Green = CLng("&H" & Mid$(strBuff, 3, 2))
        .Red = CLng("&H" & Mid$(strBuff, 5, 2))
    End With
    Conv = ccOut
End Function

Public Sub Save()
    strFile = App.Path & "\Config.ini"
    intFreeFile = FreeFile
   
        For X = 0 To 21
            strData = Split(txtsetting(X).Text & ",,,", ",")
            lngBuff = RGB(Val(strData(0)), Val(strData(1)), Val(strData(2)))
            strBuff = strBuff & Hex$(lngBuff)
        Next X
        strBuff = strBuff & txtsetting(22).Text & Chr$(&H0)
        strBuff = strBuff & txtsetting(23).Text & Chr$(&H0)
        strBuff = strBuff & txtsetting(24).Text & Chr$(&H0)
        strBuff = strBuff & txtsetting(25).Text & Chr$(&H0)
        strBuff = strBuff & txtsetting(26).Text & Chr$(&H0)
        strBuff = strBuff & txtsetting(27).Text & Chr$(&H0)
    Call Kill(strFile)
    Open strFile For Binary Access Write As #intFreeFile
        Put #intFreeFile, 1, strBuff
    Close #intFreeFile
End Sub

Public Sub Load()
    strFile = App.Path & "\Config.ini"
    intFreeFile = FreeFile
   
    Open strFile For Binary Access Read As #intFreeFile
        strBuff = String(LOF(intFreeFile), Chr$(&H0))
        Get #intFreeFile, 1, strBuff
    Close #intFreeFile
   
    For X = 0 To 21
        If LenB(strBuff) < 6 Then
            MsgBox ("OMG OMG HAXORZ CONFIG FILE IS F****** UP!!!!")
            Exit Sub
        Else
            ccColors(X) = Conv(Left(strBuff, 6))
            strBuff = Mid$(strBuff, 7)
        End If
        txtSetting(X).Text = ccColors(x).Red & "," & ccColors(X).Green & "," & ccColors(X).Blue
    Next X
   
    For X = 0 To 5
        strOthers(X) = Left(strBuff & Chr$(&H0), InStr(strBuff & Chr$(&H0), Chr$(&H0)))
        strBuff = Mid(strBuff, InStr(strBuff & Chr(&H0), Chr(&H0)) + 1)
        txtSetting(22+X).Text = strOthers(X)
    Next X
End Sub


Just ask for an explanation and you shail get it, Not this is all theroetical, None of it is tested, but it will most likly work.
Hex$(RGB(X, Y, Z)) Will always return a 6 Lengenthed string btw.
000000
<3
A HELL OF A LOT less code yes?
~-~(HDX)~-~

Proud host of the JBLS server www.JBLS.org.
JBLS.org Status:
JBLS/BNLS Server Status

Tontow

Quote from: HdxBmx27 on June 13, 2005, 08:42 PM
Dude ever heard of the nifty function RGB()?

A HELL OF A LOT less code yes?
~-~(HDX)~-~

yes, I have heard of rgb;
Quotecchatback.Red & "," & cchatback.Green & "," & cchatback.Blue
hmm, I wonder what those are values for? - Its certainly not varables for a hex number.  :P
and
  It looks like your putting all the color values into one string for saveing and then splitting them after reading and im gessing that Conv converts the rgb value to a hex (Isn't that what Hex$(RGB(X, Y, Z))  dose?) so as to use a hex value with addchat, correct?
  Yet, that code would work for storing the color values, but not the other stuff (username, password, etc) or am I reading the code wrong? (Please explain it to me if I did.)
  But I'm gessing that "ccColors(0 To 21) As CColor" (useing an array) was responsable for shrinking the code? 

  BTW, dose code that was shrank by useing an array take up less processing time and memory at run time? ; You are, after all, setting the same number of varables.

Hdx

Wel I thought you were storing the configuration in Text boxes with the formatting of:  Red,Green,Blue

I took that assumption from this:

tempstring = Split(txtsetting(1).Text, ",")
cleave.Red = tempstring(0)
cleave.Green = tempstring(1)
cleave.Blue = tempstring(2)

So I just loaded that and displayed it in the same monnor.

What Conv() does is converts the RGB hex string to your nifty little cColors type.
The  code works for storing colors and string, What I did was store all 22 color settings first, then each string was then inserted using the normal null terminator.

Quote
        strBuff = strBuff & Hex$(lngBuff) '3 bytes Hex value for the RGB color.

        strBuff = strBuff & txtsetting(22).Text & Chr$(&H0) 'Null-terminated string
        strBuff = strBuff & txtsetting(23).Text & Chr$(&H0) 'Null-terminated string
        strBuff = strBuff & txtsetting(24).Text & Chr$(&H0) 'Null-terminated string
        strBuff = strBuff & txtsetting(25).Text & Chr$(&H0) 'Null-terminated string
        strBuff = strBuff & txtsetting(26).Text & Chr$(&H0) 'Null-terminated string
        strBuff = strBuff & txtsetting(27).Text & Chr$(&H0) 'Null-terminated string

And yes using an array is what is responsible for shrinking the code. Also you *Could* just get rid of the array of types, and just use one overall vareable of that type. The array of cColors is unneeded because you have an array of textboxes.

Honestly? It ran faster then your code on my comp. But It will varrie from computer to computer. As considering each cColors type uses 3 Dwords of memory, thats not good, but you could like I said, splice it down to only using on of those types.
ccColors(0 To 21) As CColor --------> ccColors As CColor,
ccColors(X) = Conv(Left(strBuff, 6)) ----------->ccColors = Conv(Left(strBuff, 6))
txtSetting(X).Text = ccColors(x).Red & "," & ccColors(x).Green & "," & ccColors(x).Blue --------------> txtSetting(X).Text = ccColors.Red & "," & ccColors.Green & "," & ccColors.Blue

~-~(HDX)~-~
PS: Please let the grammar and spelling slide, i just woke up.

Proud host of the JBLS server www.JBLS.org.
JBLS.org Status:
JBLS/BNLS Server Status

Stealth

Quote from: Tontow on June 14, 2005, 01:28 AM
  BTW, dose code that was shrank by useing an array take up less processing time and memory at run time? ; You are, after all, setting the same number of varables.

When you talk about runtime speed, you're speaking in microseconds or, at the most, milliseconds. The difference is not noticeable to your user unless it's part of a massive (1,000+ iterations) loop. It is much more worthwhile to write clean, readable, portable code than to save your user's computer a couple milliseconds of execution time, and your original piece of code is a horrendous mess.
- Stealth
Author of StealthBot