Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable string sizes in file i/o
#1
Most probably just stupidity on my part, or lack of knowledge about the Get and Put functions.

I'm wanting to store 3 variable strings using file i/o, and here's what I got:

Storing the string sizes, then the strings themselves;
Code:
Put #f, , Clng(LenB(.name))
Put #f, , clng(LenB(.description))
Put #f, , clng(LenB(.skill))

Put #f, , CStr(.name)
Put #f, , CStr(.description)
Put #f, , CStr(.skill)

Loading the size values, then trying to load that many bytes into the string;
Code:
Get #f, , sSize1
Get #f, , sSize2
Get #f, , sSize3

Get #f, , .name
Get #f, 12 + sSize1, .description
Get #f, 12 + sSize1 + sSize2, .skill
Get #f, 12 + sSize1 + sSize2 + sSize3, .hp

The 12 is obviously there because that's the size of 3 longs (4 bytes). I then add the size of the .name to the StartByte field, meaning it should read the .name fine (Shouldn't it?) then each of the other values.

Really doesn't work though. It won't even read .name properly.

I've tried a few variations, such as loading .name from the 12th byte. I have a small recollection of VB6 being silly and starting at 1 instead of 0, unlike pretty much all languages. So would I need to do 13, and add a 1 to the sSize vals?

Any help with the code, or giving me some more understanding of the syntax would help.

Thanks.
Quote:Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?
Reply
#2
Code:
Private Sub Command1_Click()
Dim s As String
Dim s2 As String

    s = "hello"
    s2 = "world"
    
    Dim FileName As String
    Dim f As Long
        
    FileName = App.Path & "\test.test"
    
    f = FreeFile
    Open FileName For Binary As #f
        Put #f, , CLng(Len(s))
        Put #f, , s
        Put #f, , CLng(Len(s2))
        Put #f, , s2
    Close #f
    
End Sub
Code:
Private Sub Command2_Click()
Dim s As String
Dim s2 As String
Dim sLen As Long

Dim FileName As String
Dim f As Long
        
    FileName = App.Path & "\test.test"
    
    f = FreeFile
    Open FileName For Binary As #f
        Get #f, , sLen
        s = String$(sLen, " ")
        Get #f, , s
        
        Get #f, , sLen
        s2 = String$(sLen, " ")
        Get #f, , s2
        
        Debug.Print s, s2
    Close #f
End Sub
Reply
#3
Ah, so I need to set the string to the size it needs to be before reading.

Never used String$ before, but would
Code:
.name = Space$(sLen)?
have any notable performance issues compared to String$?
Quote:Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?
Reply
#4
Ok, just did a test of String$() vs Space$().

Space$() wins.

10 loops of 50,000.
Code:
%Faster    69.2|    83.3|    76.9|    69.2|    69.2|      50|    69.2|    83.3|    64.3|    57.1
Test1        22|      22|      23|      22|      22|      21|      22|      22|      23|      22
Test2        13|      12|      13|      13|      13|      14|      13|      12|      14|      14

10 loops of 10,000.
Code:
%Faster     200|     200|     200|     200|     200|     200|     200|     200|     200|     200
Test1         3|       3|       3|       3|       3|       3|       3|       3|       3|       3
Test2         1|       1|       1|       1|       1|       1|       1|       1|       1|       1

Code:
Public Sub TestOne()
Dim s As String
    s = String$(40, " ")
End Sub

Code:
Public Sub TestTwo()
Dim s As String
    s = Space$(40)
End Sub
Reply
#5
Ah, I suspected that the lack of a set character in the line syntax might have some sort of performance boost, but by the looks of things it's fairly neglible. Much obliged for the help. I'll be using Space$ loading up 3 strings per account. Once I finish off this part of the project, I'll post a speed test of Space$ vs. String$ with a large amount of data, loading 10,000 classes or something similar.

Thanks again.
Quote:Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)