| Mirage Source http://www.miragesource.net/forums/ |
|
| Variable string sizes in file i/o http://www.miragesource.net/forums/viewtopic.php?f=201&t=5279 |
Page 1 of 1 |
| Author: | Robin [ Thu Mar 19, 2009 1:02 am ] |
| Post subject: | Variable string sizes in file i/o |
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. |
|
| Author: | Jacob [ Thu Mar 19, 2009 2:18 am ] |
| Post subject: | Re: Variable string sizes in file i/o |
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 |
|
| Author: | Robin [ Thu Mar 19, 2009 6:23 pm ] |
| Post subject: | Re: Variable string sizes in file i/o |
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$?
|
|
| Author: | Jacob [ Thu Mar 19, 2009 6:34 pm ] |
| Post subject: | Re: Variable string sizes in file i/o |
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 |
|
| Author: | Robin [ Thu Mar 19, 2009 6:43 pm ] |
| Post subject: | Re: Variable string sizes in file i/o |
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. |
|
| Page 1 of 1 | All times are UTC |
| Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |
|