Valhalla Legends Forums Archive | Visual Basic Programming | Binary file reading problems....

AuthorMessageTime
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)

[code]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
[/code]
June 11, 2005, 2:32 AM
Yegg
Just a quick note, instead of having multiple
[code]tempstring = Split(txtsetting(2).Text, ",")
crecivewhisper.Red = tempstring(0)
crecivewhisper.Green = tempstring(1)
crecivewhisper.Blue = tempstring(2)
Put #50, , crecivewhisper[/code]
paragraphs of code, you can use a for loop and only use one.
June 11, 2005, 2:36 AM
Quarantine
..and how is a for loop not worse?
June 11, 2005, 2:45 AM
Tontow
[quote author=Yegg link=topic=11799.msg115408#msg115408 date=1118457374]
Just a quick note, instead of having multiple
[code]tempstring = Split(txtsetting(2).Text, ",")
crecivewhisper.Red = tempstring(0)
crecivewhisper.Green = tempstring(1)
crecivewhisper.Blue = tempstring(2)
Put #50, , crecivewhisper[/code]
paragraphs of code, you can use a for loop and only use one.
[/quote]

Are you saying that I would be better off haveing my user defined varable be something like
[code]
Private Type setting
    Red() As String
    Green() As String
    Blue() As String
    Value() As String
End Type
[/code]
and then looping through? (useing an array to store the data)
June 11, 2005, 3:20 AM
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 :-

[code]

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

[/code]

Note i haven't tested this code, it's just an example.
June 11, 2005, 3:28 AM
Dyndrilliac
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 author=Tontow link=topic=11799.msg115407#msg115407 date=1118457158][code]
'open file for random access
Open filepath For Binary As #50
[/code]
[/quote]That's just begging for an access violation. Do this:[code]FileInstance = FreeFile
Open Filepath For Binary As #FileInstance[/code]Edit: FileInstance is an Integer, by the way.
June 11, 2005, 3:45 AM
R.a.B.B.i.T
[quote author=Tontow link=topic=11799.msg115407#msg115407 date=1118457158]
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)

[code]Private Type ccolor
    Red As String
    Green As String
    Blue As String
End Type
Private Type logininfo
    Value As String
End Type
[/code]
[/quote]You have to allocate memory to strings if you're using Binary file access.
June 11, 2005, 11:16 AM
Tontow
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?)
June 11, 2005, 3:46 PM
OnlyMeat
[quote author=Tontow link=topic=11799.msg115435#msg115435 date=1118504812]
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?)
[/quote]

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.
June 11, 2005, 8:24 PM
R.a.B.B.i.T
[quote author=Tontow link=topic=11799.msg115435#msg115435 date=1118504812]
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?)
[/quote]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.
June 11, 2005, 8:38 PM
Stealth
[quote author=rabbit link=topic=11799.msg115454#msg115454 date=1118522339]
[quote author=Tontow link=topic=11799.msg115435#msg115435 date=1118504812]
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?)
[/quote]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.
[/quote]

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..
June 13, 2005, 11:41 PM
HdxBmx27
Dude ever heard of the nifty function RGB()?

Also I hate prinintg types to files so here:

[code]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[/code]

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.
[color=Blue]00[/color][color=Green]00[/color][color=Red]00[/color]
<3
A HELL OF A LOT less code yes?
~-~(HDX)~-~
June 14, 2005, 1:42 AM
Tontow
[quote author=HdxBmx27 link=topic=11799.msg115665#msg115665 date=1118713346]
Dude ever heard of the nifty function RGB()?

A HELL OF A LOT less code yes?
~-~(HDX)~-~
[/quote]

yes, I have heard of rgb; [quote]cchatback.Red & "," & cchatback.Green & "," & cchatback.Blue[/quote] 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.
June 14, 2005, 6:28 AM
HdxBmx27
Wel I thought you were storing the configuration in Text boxes with the formatting of:  Red,Green,Blue

I took that assumption from this:
[code]
tempstring = Split(txtsetting(1).Text, ",")
cleave.Red = tempstring(0)
cleave.Green = tempstring(1)
cleave.Blue = tempstring(2)[/code]
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][code]
        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
[/code][/quote]

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.
[code]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
[/code]
~-~(HDX)~-~
PS: Please let the grammar and spelling slide, i just woke up.
June 14, 2005, 3:16 PM
Stealth
[quote author=Tontow link=topic=11799.msg115691#msg115691 date=1118730523]
  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.
[/quote]

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.
June 15, 2005, 2:41 AM
Tontow
  So I'm better off haveing cleaner and smaller code; I'll see what I can do to clean it up.

Though, one thing keeps evadeing me:  Where is it possable to declear a Public user type? - It keeps saying that it has to be private.....
June 15, 2005, 4:18 AM
Stealth
Public-scope variables can be declared only in modules and class modules. I think.
June 16, 2005, 12:14 AM
Tontow
I think thay lied about being able to do it in class modules, I ended up haveing to use a regular module..
June 16, 2005, 12:31 AM
QwertyMonster
I have done it in a form, public module and class module.
June 16, 2005, 2:59 PM

Search