Results 1 to 9 of 9

Thread: opening a file as binary.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2000
    Posts
    215
    Hiyas,

    I need to open a whole file for binary, and add each byte to a text box, but when I try the EOF method it just seems to keep going after the file is done, and get more binary, and also it is incredibly slow method of opening the file, can someone tell me how to get the whole file for binary and dump it all into a text file?

    and if you had a string full of bytes, how would you open the file for binary and chuck them all in a new file, with out having to put the bytes in 1 by 1?

    thanx for any help.

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Clarification

    Some questions that will guide you to the best solution:

    Is the file a known length?

    Does the file really contain binary data (of which text is certainly a subset) or is it only ever containing ASCII characters (i..e text)? By the fact that you want to populate a text box with the byte array I am assuming simple ASCII data here

    No matter what the answers are, there is a way to open the file and read it without having to read the bytes one at a time.

    regards
    Paul Lewis

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    EOF doesn't work with binary use Loc(filenumber)>lof(filenumber)

    And for how to dumping the text to file and vice versa;

    Code:
    'in code
    'To load text
    text1=file(yourfilename)
    'To write text
    file(yourfilename)=text1
    
    'in module or whereever
    Property Get File(Filename As String) As String
        Dim fnum As Byte
        fnum = FreeFile
        Open Filename For Binary As fnum
            File = Space(LOF(fnum))
            Get #fnum, , File
        Close fnum
    End Property
    Property Let File(Filename As String, textstring As String)
        Dim fnum As Byte
        fnum = FreeFile
        If Dir(Filename) <> "" Then Open Filename For Output As fnum: Close fnum
        Open Filename For Binary As fnum
            Put #fnum, , textstring
        Close fnum
    End Property
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2000
    Posts
    215
    I think I'm doing something wrong, I am using this type of code but it takes forever to load all the bytes into the textbox

    Code:
    Text1 = ""
    Dim i As Long
    Dim Bin As Byte
    Open "C:\windows\desktop\file.exe" For Binary As #1
    Do While Not Loc(1) > LOF(1)
    i = i + 1
    Get 1, i, Bin
    Text1 = Text1 & Bin
    DoEvents
    Loop
    and I tried your code Kedaman for dumping the file in a text file, but it only put the first 3 characters that you would see if you opened a text file in notepad, into the textbox.

    [Edited by Crypt on 07-18-2000 at 10:53 PM]

  5. #5
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Hmmm

    Well it looks to me like you are attempting to read binary data however you are attempting to place this in a text box which won't let you see all the data.

    Is that your intention?

    kedaman's code works perfecty given the assumption that you are transferring text between the file and the text box. You said that it only shows the first three characters? Well that's because the fourth character is a 0 in your file.

    Notepad handles the character 0 more cleverly by assuming you want to see as much of the file as possible, so it places a space where the chr(0) was. Just try opening your file in notepad, then saving it off as another name and see if it still works. You will find it doesn't. Furthermore, if it is an executable as your example suggestes, it may crash your system.

    So I think you must decide what you want to do about these non ASCII characters. If you want them replaced with something meaningful (like a space) and then put in your text box, then you will have replace each of these characters after initially reading in the string.

    If you think you will be able to change anything in the file and save it again without corrupting it...you better think again. As soon as you change those chr(0)'s to something else, you have ruined your chances of saving the file as it was.

    Are you trying to make a Hex editor or something?

    Regards
    Paul Lewis

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2000
    Posts
    215
    yeah my intention is to dump it all into a text box, so is there any control which does supports the chr(0)? thanx for the info paul.

    Nope I am not trying to make a hex editor, I am trying to transmit a file through winsock but something goes wrong in the transmit of the data, the file is only small (12kb) than I am trying to send, I wrote the code myself to open the file and save it, but I thought I was doing something wrong with that part of the code because when the file is saved after it is finished being recieved it is corrupt, Kedamans code for reading the file was the same as mine except mine was a function and I was filling in the string with the chr(0) instead of the space character, so I thought maybe that was what I was doing wrong so I changed it to the space, but the file still ended up corrupt, for saving I was just saving the file by just opening a file for output and printed the data that was recieved (it worked printing it for output, I tested it by just opening a file then printing it to a different location and the file worked). Anyway I was trying to open the corrupt file and the orignal and print them each to a different text file, and then find the first difference between the two so I could see where abouts it was stuffing up and if it stuffed up again after that.

    Please don't tell me to check the example on vb-world I am trying to write my own from scratch.


  7. #7
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    I'd put it in a byte array though. This loads megs per second for me!

    eg:

    Code:
    Dim Filename as string
    Dim Bytes() as byte
    
    Filename = "C:\Myfile.exe" 'or input from common Dialog etc
    redim Bytes(FileLen(Filename))
    
    open Filename for Binary as #1 '(better to use freefile I know)
    Get #1, ,Bytes
    Close #1
    
    ' check data (it'll slow it down in IDE mode though)
    Debug.print StrConv(Bytes, vbUnicode)
    
    'Send data
    Winsock1.SendData Bytes
    I didn't run this I just wrote it here so I appologise if a spelling error causes it not to work.






    [Edited by Paul282 on 07-19-2000 at 04:24 AM]
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Yeah forgot to tell you, but textboxes are for text, and nullchar means end character in the textbox.

    I've done a hex editor, it's on my homepage if you want to play round with it, if you need the text to appear like ascii you should use print command, you could also develope a textbox without the nullchar problem
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2000
    Posts
    215
    thanx, I now have it loading into a textbox and found where my problem was, I replaced chr(0) with the 6 digits "chr(0)" in the textbox, so I knew where each chr(0) was, I didn't really need to save the file, because I had to find where the file was getting corrupt just don't know why, anyway here is the code which will load it into a textbox and still allow you to save the file afterwards in anyone actually wants it.

    Code:
    Public Function OpenFileToTextBox(Filename As String, txtbox As TextBox) As String
    
        Dim temp As String
        Dim FileHolder As String
               
        Open Filename For Binary Access Read As #2
            FileHolder = String(FileLen(Filename), Chr(0))
            Get #2, , FileHolder
        Close #2
      
      If InStr(1, FileHolder, Chr(0)) <> 0 Then
        
    temp = FileHolder
    FileHolder = ""
    Do Until InStr(1, temp, Chr(0)) = 0
    FileHolder = FileHolder & Mid$(temp, 1, InStr(1, temp, Chr(0)) - 1) & "chr(0)"
    temp = Mid$(temp, InStr(1, temp, Chr(0)) + 1)
    DoEvents
    Loop
    End If
    txtbox.Text = FileHolder
    End Function
    
    Public Function SaveFile(Filename As String, txtString As String)
    Dim temp As String
    Dim FileHolder As String
    FileHolder = txtString
    If InStr(1, FileHolder, "chr(0)") <> 0 Then
    temp = FileHolder
    FileHolder = ""
    Do Until InStr(1, temp, "chr(0)") = 0
    FileHolder = FileHolder & Mid$(temp, 1, InStr(1, temp, "chr(0)") - 1)
    FileHolder = FileHolder & Chr(0)
    temp = Mid$(temp, InStr(1, temp, "chr(0)") + 6)
    DoEvents
    Loop
    FileHolder = FileHolder & temp
    End If
    
    
    Open Filename For Output As #1
    Print #1, FileHolder
    Close #1
    
    End Function

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