Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grabbing variable length string from binary file

Status
Not open for further replies.

dejfatman

Programmer
Jan 24, 2002
34
US
I'm a vb newbie, and I am trying to retrieve some variable length strings (City names) from a binary file. The binary file contains data from which I can determine the strings' locations and lengths. Right now, I am just trying to display the data, but I'm having problems. My code is as follows:

Dim strName() as String
...
While...
MsgBox "nameLen=" & nameLen
ReDim strName(1 to nameLen)
Get #nFileNum, namLoc, strName
MsgBox "strName=" & strName
...
Wend

Now, for my first retrieval in my test, I am getting "nameLen=10", and then "strName=Cleveland", which looks right. For the next retrieval, I am "nameLen=6", and then "strName=DallasV<||...(etc.)", which is all messed up. Can someone tell me what I'm doing wrong? By the way, if I try to make my second display 'MsgBox strName & "<some other text>", the <some other text> does not show up for either retrieval. Basically appending text to the field name does not work in any case. Any ideas?
 
I'm not sure that I understand quite what you are upto:

Why do you redim the line each time:
ReDim strName(1 to nameLen)
This makes the variable into an array, of size nameLen. This will therefore hold hold nameLen number of strings, each of which could be as large as you like. It does not, create an array, and then place one letter in each part of the array. If this is all you are doing eith this variable then you only need a single string varaible.

DIm strName as string

Try this, and remove the redim line. Note the lack of () in the declaration line above.

Also, do you always get the name of the city followed randow characters? If so, then you could use the left, or mid function to truncate the string, and only return the name, as you know the variable length.

BB
 
Thanks, you are right about the array of strings. I was being stupid, because what I was really trying to make an array of characters. I've changed it to an array of bytes instead, and I am getting what I want now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top