|
-
July 11th, 2010, 11:55 AM
#1
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 !
-
July 11th, 2010, 01:20 PM
#2
Re: VB6 How to Read text/String Using binary method ?
No. It's not a good idea to mess with binary data.
-
July 11th, 2010, 01:51 PM
#3
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.
-
July 11th, 2010, 06:51 PM
#4
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
-
July 12th, 2010, 12:34 AM
#5
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...)
-
July 12th, 2010, 06:53 AM
#6
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.
-
July 12th, 2010, 08:13 AM
#7
Re: VB6 How to Read text/String Using binary method ?
 Originally Posted by DataMiser
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|