Opening files in Binary Mode
I want to be able to open up binary files and dump it's contents into a Ritch Text Box. I made a program to do this, but it doesn't display the ASCII character (0).
I'm using a RitchTextBox; multiline = true.
VB Code:
Open App.Path & "\scripts\" & fileScript.FileName For Binary As #3
Do Until EOF(3)
Line Input #3, cmd
txtMessage.Text = txtMessage.Text & cmd
Loop
Close #3
It just skipps the ASCII Values of (0). I made sure that the text box can even display the ascii value (0) by using
and it displayed it fine. So that means their is something wrong when inputting from the file. What am I doing wrong?
Re: Opening files in Binary Mode
I just tried rtb1.Text = Chr(0) and I get an outofmemory error..
Re: Opening files in Binary Mode
Quote:
Originally Posted by [A51g]Static
I just tried rtb1.Text = Chr(0) and I get an outofmemory error..
rtb1.Text = "something" & chr(0).
Sorry, my bad... You need something before it or else it doesn't work. :/
Re: Opening files in Binary Mode
ok ;) lol...
do you need just the first line of the text file or is it only one line?
Re: Opening files in Binary Mode
I want the whole text file... all of it.
Re: Opening files in Binary Mode
I'm not sure how to fix it but I believe the problem is being caused by the fact that VB uses chr(0) as the end-of-file marker for binary files. Perhaps you could read the file as non-binary and then convert it before you show it in the RTB.
Re: Opening files in Binary Mode
I'm surprised that no one solved this yet...
Open a file, and display the contents of a file in a text box, even the ASCII(0) character. Thats all I want, why does it have to be so hard.
Re: Opening files in Binary Mode
Please attach one of your files that has this problem.
Re: Opening files in Binary Mode
hmm that's odd I can load chr(0)'s into richtextboxes with no problems
perhaps it's because you're loading line by line (which may chop chr(0)'s)
try loading it this way
VB Code:
Dim strFile As String, ff As Long, Buffer() As Byte
strFile = "c:\test.txt" '83mb text file filled with random characters and many chr(0)'s through out.
ff = FreeFile 'grab a freefile
Open strFile For Binary As ff 'open the text file in raw binary mode (very fast)
ReDim Buffer(LOF(ff)) 're-dimension buffer which is a byte array
Get ff, , Buffer() 'load the contents of the file into buffer (half the space, half the time)
Close ff 'close the file
Me.RichTextBox1.Text = StrConv(Buffer, vbUnicode)
btw this loads the 83mb text file into the rtb in less than 9ms
hope this helps
1 Attachment(s)
Re: Opening files in Binary Mode
File attached.
It will display everything... and just skip the ASCII(0) characters.
*NOTE* Open it with a Hex Editor. If you view it in notepad you can't really tell what the ASCII characters are. Also, if you save the file in notepad even though you didn't change anything, the NULL characters will be replaced with other characters, so don't save it... just view it.
Re: Opening files in Binary Mode
Quote:
Originally Posted by Fiend
File attached.
It will display everything... and just skip the ASCII(0) characters.
*NOTE* Open it with a Hex Editor. If you view it in notepad you can't really tell what the ASCII characters are. Also, if you save the file in notepad even though you didn't change anything, the NULL characters will be replaced with other characters, so don't save it... just view it.
your file loaded fine with my code from above
I can see chr(0)'s in your document
Re: Opening files in Binary Mode
Fiend,
What you want to do is like a hex file editor. Nothing new. You just can't do it in the manner you expect. Look in my signature and you will find one. There are a few of this program out there.
BTW: All binary character representations are not ASCII.