I sure hope I can get help with this. I seem to have a simple problem. I wrote an entire program without a single problem with Visual Basic 2005 Express but now I'm having a little trouble. When I read string data from a binary file I get "string (no closing quote) This presents a problem when I do a string comparison or when I try to put string1 and string2 on the same index line of a listbox. (You only see string1). I've tried so many different things including using fixedlengthstring but that simply caused more problems. Lets say I want to read a string that is 19 letters long starting at location 1 in the binary file, how do I do it and get both opening and closing quotes: "string" instead of "string I hope I've been clear enough that someone can get me on the right track here. Thanks 4 your time. ![]()

Reading a string from binary file results in "string
jroggen
Max Vernon
Spotty,
That didn't solve the problem but it was helpful because it gave me some useful information on what's happening. When i was = 4, s was "Steve" (Exactly what I wanted) but after each next i, s was = to "Steve (no closing quote) After for was finished there was no change to the string with ItemToAdd = s.trim
I've looked into this problem for several days and have seen that others are running into this same problem reading string information that was saved into binary files. I'm clueless on this one
Any ideas
Francis Shum
The filename was contained in the textbox1.text property.
http://msdn2.microsoft.com/en-US/library/1k0307eb.aspx
To read first 19 bytes and display them in a listbox item. Use of trim will remove the whitespace characters - Ie. the trailing spaces.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim B As Byte()
B = My.Computer.FileSystem.ReadAllBytes("c:\ccchm.dt")
'//Look at only the first 18 Bytes in the Byte Array
Dim s As String = ""
For i = 0 To 18
s = s & Chr(B(i))
Next
Dim ItemToAdd as string = ""
ItemToAdd = s.trim
Listbox1.Items.Add (ItemToAdd )
End Sub
End Class
AL...
So I need to be clear on what you are telling me the bytes are as follows
1 = "
2 = S
3 = t
4 = e
5 = v
6 = e
7 = Nothing - Value of 0
8 = Nothing - Value of 0
..
Is this representative of what your telling me. Or is there something more.
And when you read these bytes you get a string or character array
"Steve
But you want a string that is "Steve" - with a closing quote. Is this closing quote anywhere in the byte array. Do you want to include the quotes in the final string or not.
A character of value 0 in a string will cause the string to be ended at that point so if youve got something like a string with the following
"Steve xyx" and between the e and the x is a character with a value of 0 then this would cause the string to only display "Steve - you would need to replace the character 0 with a Space (character 32).
I really need clarity on what is in the contents of the file and what you are trying to achieve
Rajashekar
Spotty, thanks for the quick reply. I tried the code you sent but couldn't figure out where to put the file name to read the bytes from. Also it didn't seem to be what I'm looking for.
Renee, here is the code you requested:
-MODULE-
Public firstname As String-MAIN FORM-
Dim strloc As Integerfirstname = " " '19 spaces
strloc = 1
FileOpen(1,
"c:\ccchm.dat", OpenMode.Binary)FileGet(1, firstname, strloc)
'File information was written using the exact procedure with fileput
------------------------END OF CODE-----------------------------------
The data for this file, starting at location 1, is "Steve "
When I read this file information starting at 1, firstname = "Steve
If I try to put firstname & lastname into a listbox I just see Steve; This is probably because there is no defined end to the string firstname. Therefore I simply need some way of converting firstname = "Steve to firstname = "Steve" (or whatever firstname happens to be)
If either of you could still assist I'd be very thankful
Maverick2311
If you want that additional whitespace then you can use the replace method to convert the null values into spaces. Sometimes this may be useful other times its not.
Varadca
Cassiopia,
I'm sorry. I'm rushed for time.
What does the first name bytestream look like
Read it in a byte array and encode it into a string.
This should do it.
Bill G
Spotty,
You are absolutely correct. Just minutes ago, I came to the same conclusion that I'm reading all nothing bytes after the "Steve (Byte location 5 to 18 are all nothing. I then concluded that saving it not as "Steve" but rather as "Steve " resolved the problem.
Thank you both very much.
This problem is now resolved...
Drunkalot
OK I'm still not quite sure what you asking for with regard to the " marks but if you trying to read specific bytes from a binary file like you said location 1 to 19 then thats easily done.
The following shows reading a file into a byte array - at which point you can choose to examine the bytes and in this case form a string, or just output the bytes. If you run this at an text file then it will display the characters.
So looking a specific positions to get byte value and turning it into a string is easily achieved.
As to what you problem is with string quotations - I'm not sure if this is the same or a different problem Are you wanting to read the byte stream until you find the terminating quotation.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim B As Byte()
B = My.Computer.FileSystem.ReadAllBytes(Textbox1.Text)
'//Look at only the first 18 Bytes in the Byte Array
Dim s As String = ""
For i = 0 To 18
s = s & Chr(B(i))
Next
Console.WriteLine(s)
'//Write Out all the Byte Values
For Each x As Byte In B
Console.Write(Chr(x))
Next
End Sub
End Class