• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

## Recommended Posts

Hi. Sorry if this was already answered somewhere else, but all my searches were of no help. I'm trying to store, via "Put," a byte array (a string) to a file and recover it. Here's a small program that should, afaik, produce these results: Dim saveText() As Byte Dim loadText() As Byte ReDim saveText(11) As Byte ReDim loadText(11) As Byte saveText = "Hello World" Open "test.txt" For Binary As #1 Put #1, , saveText() Close #1 Open "test.txt" For Binary As #1 Get #1, , loadText() Close #1 MsgBox saveText() MsgBox loadText() The output is a msgbox with the caption "Hello World" followed by a msgbox with the caption "Hello." Can someone please tell me wtf is going on here? -Brian -------- EDIT Check out the contents of "test.txt": H e l l o W o r l d Why does it store a character, a blank space, a character, etc.? And why won't it load it like that? Edited by - shawncorn on January 1, 2002 9:36:19 PM

##### Share on other sites
I think the problem is because VB strings are unicode, so when loaded into the byte array like that, it only gets half of it. Or something like that, I''d never seen a Byte array used as a string like that before, so I''m playing around with it now.

For a function which loads a file to a string, here is a known working one:

End If
Next
Close #1

##### Share on other sites
A better way to load the whole thing would be like this:

Dim Data() As ByteDim FileNum As LongFileNum = FreeFileOpen FileName For Binary As FileNumRedim Data(LOF(FileNum) - 1)For i = 0 To LOF(FileNum) - 1 Get FileNum, i + 1, Data(i)NextClose FileNum

Now, while that will read everything just fine, if you then choose to use that Byte array as a string, you will need to write another function:

Function ByteArrayToString(ByteArray() As Byte) As String ''Takes a byte array and interprets it as an ASCII string Dim i As Long ByteArrayToString = Space$(UBound(ByteArray) + 1) For i = 0 To UBound(ByteArray) Mid$(ByteArrayToString, i, 1) = Chr$(ByteArray(i)) NextEnd Function  Again, this only builds it one byte at a time (which is unavoidable), but the line: ByteArrayToString = Space$(UBound(ByteArray) + 1)

Will make the string big enough to fit the whole string all without being resized. (I was writing this code from my head, so if Space$isn''t a function, then sorry... I think there is a function which will fill a string with n characters... try finding that) Taking a step back though, here are some more simple ways to do it: Dim Blah As String Open "c:\test.txt" For Binary As #1 Blah = Space$(LOF(1))    Get #1, 1, Blah    Close #1   Debug.Print Blah

Again, note the Space$, it makes the string so it can fit the file, because otherwise, the Get statement doesn''t know how much to read... update, Space$ is a function , and this code works fine, even with carriage returns.

Or, if you want, you could use text mode:

Dim Blah As StringOpen "c:\test.txt" For Input As #1Line Input #1, BlahClose #1

That will read a single line and put it into Blah. Use this one with EOF to read an entire file.

So yeah, my recommendation would be to use the last binary mode method I showed, it will be the fastest of all the methods.

Trying is the first step towards failure.

##### Share on other sites
One final thing: don't be lazy like I was with those last few examples... the "best" code should not assume that the file handle 1 is available, and it should also be written as a proper function:
Function ReadFile(FileName As String) As String On Error Goto ErrorHandler Dim FileNum As Long FileNum = FreeFile  Open FileName For Binary Access Read Lock Read Write As FileNum ReadFile = Space\$(LOF(FileNum))  Get FileNum, 1, ReadFile Close ReadFile Exit FunctionErrorHandler: 'Whatever makes you happy hereEnd Function

Trying is the first step towards failure.

Edited by - ragonastick on January 1, 2002 10:28:09 PM

##### Share on other sites
Thanks dude, I appreciate the help. Another look at MSDN helped me come up with the solution I''ll probably be using in the practical application that led me to this problem:

Open the file as binary
ByteArrayBuffer = InputB(2 * length of string, FileNum)
Close

It''s still a little weird, I really think Unicode is unnecessary.

##### Share on other sites
Especially since you cant actually use the extra characters you get, unless you use a dynamic array of bytes that is. So really, there is not much point.

Trying is the first step towards failure.

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627665
• Total Posts
2978524

• 10
• 10
• 12
• 22
• 13