Results 1 to 12 of 12

Thread: Opening files in Binary Mode

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    15

    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:
    1. Open App.Path & "\scripts\" & fileScript.FileName For Binary As #3
    2. Do Until EOF(3)
    3.   Line Input #3, cmd
    4.   txtMessage.Text = txtMessage.Text & cmd
    5. Loop
    6. 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
    VB Code:
    1. rtb1.Text = Chr(0)
    and it displayed it fine. So that means their is something wrong when inputting from the file. What am I doing wrong?

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Opening files in Binary Mode

    I just tried rtb1.Text = Chr(0) and I get an outofmemory error..
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    15

    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. :/

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    15

    Re: Opening files in Binary Mode

    I want the whole text file... all of it.

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    15

    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.

  8. #8

  9. #9
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    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:
    1. Dim strFile As String, ff As Long, Buffer() As Byte
    2.     strFile = "c:\test.txt" '83mb text file filled with random characters and many chr(0)'s through out.
    3.     ff = FreeFile 'grab a freefile
    4.     Open strFile For Binary As ff 'open the text file in raw binary mode (very fast)
    5.     ReDim Buffer(LOF(ff)) 're-dimension buffer which is a byte array
    6.     Get ff, , Buffer() 'load the contents of the file into buffer (half the space, half the time)
    7.     Close ff 'close the file
    8.     Me.RichTextBox1.Text = StrConv(Buffer, vbUnicode)

    btw this loads the 83mb text file into the rtb in less than 9ms

    hope this helps
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    15

    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.
    Attached Files Attached Files
    • File Type: txt 1.txt (59 Bytes, 55 views)

  11. #11
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    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
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  12. #12
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width