CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2010
    Posts
    4

    Smile VB6 How to Read text/String Using binary method ?

    Private Sub Form_Load()
    Dim q As String, txtFileLen As Long, bytes() As Byte
    i = FreeFile
    Open "c:\a.exe" For Binary As i
    txtFileLen = LOF(i)
    ReDim bytes(1 To txtFileLen)

    Get #i, , bytes
    Close i

    text1.text = bytes ' Here how to show binary to text field

    Open "c:\b.exe" For Binary As i
    Put #i, , bytes
    Close i

    End Sub


    actually i want to add a string before the bytes something like this
    bytes = "Fixed !" & bytes
    can i add string to bytes(0) at zero position, is there any way to open an .exe file and add some string before it. which prevent exe from running and when i try to open just remove string before exe file and write whole new exe starting before my string..... any idea !

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: VB6 How to Read text/String Using binary method ?

    No. It's not a good idea to mess with binary data.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VB6 How to Read text/String Using binary method ?

    You can read a binary file or any part of it into a string with ease. To do so you must first initialize the string var to the size you want it to hold.

    Code:
    Dim MyString as string
    
    Open "C:\MyFile.Bin" for Binary as #1
    MyString=Space(lof(1))
    get #1,,MyString
    Close #1
    This will read the entire file into the string var.

    eta: This is assuming that the file in question is under 2 gigs in size
    Last edited by DataMiser; July 11th, 2010 at 01:53 PM.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Mar 2010
    Posts
    26

    Re: VB6 How to Read text/String Using binary method ?

    [QUOTE
    actually i want to add a string before the bytes something like this
    bytes = "Fixed !" & bytes
    can i add string to bytes(0) at zero position, is there any way to open an .exe file and add some string before it. which prevent exe from running and when i try to open just remove string before exe file and write whole new exe starting before my string..... any idea ![/QUOTE]


    Try This Then

    Code:
    Dim LitFile() As Byte
    Dim FN As Integer
    Dim xlof As Long
    Private Sub Command1_Click()
    FN = 1
    Open "C:\Temp\Little.txt" For Binary As FN
    xlof = lof(FN)
    ReDim LitFile(xlof)
    Me.Print "File Size in Bytes " & xlof
    Get #1, , LitFile
    Open "C:\Temp\Little2.txt" For Binary As #2
    Put #2, 1, "Fixed !"
    Put #2, 8, LitFile
    Close #2
    Close FN
    MsgBox "Finito"
    End Sub

  5. #5
    Join Date
    Apr 2009
    Posts
    394

    Re: VB6 How to Read text/String Using binary method ?

    DataMiser...
    Code:
    Dim FileContents As String, FName As String, FNumb As Integer
    
    FName = "C:\z\test.txt"
    FNumb = FreeFile
    
    Open FName For Binary As #FNumb
    FileContents = Input(FileLen(FName), FNumb)
    Close #FNumb
    no need to allocate space prior to reading the contents in (and yes, it is also bound by the 2GB limit...)

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VB6 How to Read text/String Using binary method ?

    The method I showed can get any number of bytes from the file [up to the max allowed of course] based on the size of the string. If you were to set the string to 10 characters it would read 10 characters at a time.

    I had forgot about the input method as shown above. I think the last time I used that was GWBasic.

    Thanks for the reminder.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Jul 2010
    Posts
    4

    Cool Re: VB6 How to Read text/String Using binary method ?

    Quote Originally Posted by DataMiser View Post
    You can read a binary file or any part of it into a string with ease. To do so you must first initialize the string var to the size you want it to hold.

    Code:
    Dim MyString as string
    
    Open "C:\MyFile.Bin" for Binary as #1
    MyString=Space(lof(1))
    get #1,,MyString
    Close #1
    This will read the entire file into the string var.

    eta: This is assuming that the file in question is under 2 gigs in size
    That is one for which i am looking for thanks all of you Each you posted fits best

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

Featured