It's a tad more complicated than that. If I recall correctly...
When storing variable-length strings from a structure to a file,
VB saves the length as a WORD... unless the length is greater
than or equal to 65535. In that case, it stores 65535 (&HFFFF),
followed by the length as a LONG.
------------------
Tom Hanlin
PowerBASIC Staff
Announcement
Collapse
No announcement yet.
Reading in Random access mode
Collapse
X
-
Fred, my apologies - youre absolutely correct with that.
Yes my original random file was created in VB and used variable-length strings... from what I have learnt from this thread and my own testing, there seems to be virtually no advantage to using variable length - just the disadvantage of making it incompatible with PB, so from now on I'll always be declaring the size of my strings in VB the same as I would in PB, there'd be no hassles then. Unfortunately it's a bit late for me now, but at least it won't happen again
Even though it's a VB issue, PB staff may want to add that to the Visual Basic notes section as a lot of VB programmers use variable length strings in UDTs without a second thought
Thankyou all for clarifying this issue
[This message has been edited by Wayne Diamond (edited October 02, 2000).]
Leave a comment:
-
Wayne, as I see it, it is only the last record in the file that will be shorter than the rec-len.
All other records will be written on its boundary:
If you specify Len=100 then records will be written like this
Rec 1 FileByte 1,
Rec 2 FileByte 101
Rec 3 FileByte 201 and so on...
Rec n FileByte (n-1*100)+1 and LOF will be ((n-1*100)+1)+Len(LastUDT)
IMHO you cannot 'squeeze' more than 1 UDT per record in files opened "For Random"
I take it, your previous VB-example created the file
------------------
Fred
mailto:[email protected][email protected]</A>
http://www.oxenby.se
[This message has been edited by Fred Oxenby (edited October 02, 2000).]
Leave a comment:
-
Lance, I can see why the variable-length strings are causing problems, but I do kinda like that style of random access, simply because it's economical on disk - the written entry is only as big as it has to be (+2 bytes) - there's no whitespace (fatspace?), so even if i declare Len = 180, i can squeeze several records into the first 180 bytes and it's no drama, whereas 3 records in PB would occupy the full 540 bytes if im not mistaken?
------------------
Leave a comment:
-
Originally posted by Lance Edmonds:
Does VB truncate the UDT data if the variable length strings within the UDT cause the UDT to exceed the (in this case 100 byte) record length?
If your UDT exceed the Len-value specified, you will get Error 59
and no record written.
------------------
Fred
mailto:[email protected][email protected]</A>
http://www.oxenby.se
Leave a comment:
-
VB's variable length strings in UDT's create all sorts of problems when porting code... IMHO they are a kludge!
Question for the VB buffs: Does VB truncate the UDT data if the variable length strings within the UDT cause the UDT to exceed the (in this case 100 byte) record length?
My understanding is that (in memory) VB only stores a pointer (within the UDT) to the Unicode string data located elsewhere in memory, but VB "has" to dynamically build a UDT structure when writing the data to disk (doing the Unicode->Ansi conversions at the same time). Can someone give me specifics on the rules VB follows for this operation? Thanks!
------------------
Lance
PowerBASIC Support
mailto:[email protected][email protected]</A>
Leave a comment:
-
If VB has created the file like you show us,
You should still open it in PB for random access Len=100
Then you create a string 100 bytes long to read the complete record
Code:REC$=Space$(100) GET #1,RECNO,ABS REC$ LenName% = CVI(REC$) TheirName$ = Mid$(REC$,2,LenName%) LenAdress% = CVI(REC$,LenName%+2) TeirAdress$ = Mid$(REC$,LenName%+4,LenAdress%) PosPhoneNr% = LenName%+LenAdress+4 PhoneNr$ = Rtrim$(Mid$(REC$,PosPhoneNr%,10),any Chr$(0,32))
Fred
mailto:[email protected][email protected]</A>
http://www.oxenby.se
[This message has been edited by Fred Oxenby (edited October 02, 2000).]
Leave a comment:
-
Lance,
I just did a couple of tests then, and youre correct about the two-byte index before the field... I was hoping to use random access, but it seems i will still be able to do it in binary with just a bit of extra coding, so thankyou Eric and Lance for clarifying that for me
------------------
Leave a comment:
-
IIRC, VB saves these types of strings with a 2 byte "prefix" that indicate the length of the following variable-length string. Therefore, if the string was 10 bytes long, VB saves 12 bytes.
Therefore, you'll most likely need to use binary access in PB as Eric indicated.
------------------
Lance
PowerBASIC Support
mailto:[email protected][email protected]</A>
Leave a comment:
-
Eric, im using the LEN field, but i dont think I phrased my question very well ...
take this example:
Code:Private Type MyEntry TheirName As String TheirAddress As String PhoneNumber As String * 10 End Type Private Sub Form_Load() On Error Resume Next Dim NextEntry As MyEntry Open "c:\random.bin" For Random Access Write As #1 Len = 100 NextEntry.PhoneNumber = "9555-5555" NextEntry.TheirAddress = "My address" NextEntry.TheirName = "Joe Bloggs" Put #1, 1, NextEntry Close #1 End Sub
------------------
Leave a comment:
-
That's what the OPEN statement's LEN parameter is for. You use LEN to tell PB how long the records are, and then (per the GET documentation) you can use a variable length string variable to hold the data.
The TYPE is not necessary, simply use a string variable. Unless, of course, the TYPE is really more complex than just that one element.
If it is more complex than that, or if you are saying that the record lengths within the file are not consistent, then the file was not created as a true random-access file, and you will need to use OPEN... FOR BINARY and GET$.
-- Eric
------------------
Perfect Sync: Perfect Sync Development Tools
Email: mailto:[email protected][email protected]</A>
[This message has been edited by Eric Pearson (edited October 02, 2000).]
Leave a comment:
-
Reading in Random access mode
i have a VB program that saves records to a file in random access mode, the type is something like this:
Code:Private Type MyRecord TheirName As String End Type
Without changing the source code to the VB program, is there any way I can read that file with PB? PB wants a fixed-length string, eg STRING * 255 ... is there a way around this? unfortunately I cannot modify the VB source, the workaround has to be in my PB program
------------------
Tags: None
Leave a comment: