• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1556
  • Last Modified:

Read Binary input, find and replace string-a constant problem

Hi guys, I've already posted a similar question and it was similar but now im stuck again and i just dont understand.

I'm reading an input file, FEENG.LOC, it can be downloaded from my shared folder

http://home.ripway.com/2004-4/104687/file/index.html

I want to search for a particular word, and replace it with another. This is not a regular ascii text file.

its starts off with a lot of jiberish and then contains words.

my code is

Private Sub Command5_Click()
     Dim find As String
     Dim replaced As String
     Dim final As String
     Dim BFreeFile As Integer
     Dim ending As String

location = App.Path & "\FEENG.LOC"
BFreeFile = FreeFile
Open location For Binary Access Read As #BFreeFile
entirefile = InputB(LOF(BFreeFile), 1)
Close #BFreeFile

find = " Y a n k e e s"
replaced = "F u z k e e s"
final = Replace(entirefile, find, replaced, 1, -1, vbBinaryCompare)
ending = StrConv(final, vbUnicode)
'''''''''''''''''''''''''

location = App.Path & "\zyyy.txt"
BFreeFile = FreeFile
If Dir(location) <> "" Then Kill (App.Path & "\zyyy.txt") 'to ensure that its written
Open location For Binary Access Write As #BFreeFile
Put #BFreeFile, , ending
Close #BFreeFile

End Sub

Any help would be greatly appreciated. Thanks.
0
fuzzyfluid
Asked:
fuzzyfluid
1 Solution
 
ajexpertCommented:
Hi,
  can you please check the url.  It says file not found
0
 
Farzad AkbarnejadCommented:
I can't download FEENG.LOC file from your website.

-FA
0
 
OHDev2004Commented:
hey man i've already answered it ,, here it is :)

Private Sub Form_Load()
'its now written in the shape of a function so you can call
'it and specify different parameters if you like
'the parameters are :
'TxtSource --> the source file to read from
'TxtDestination --> the Destination file to write to
'TxtTxtFind--> the text to be searched for
'TxtReplace--> the text to be replaced
Call MyFunction(App.Path & "\Zoobhrt.txt", App.Path & "\zyyy.txt", "N e w   Y o r k   Y a n k e e s", "N e w   Y o r k   T H I S W O R K S")

End Sub


Private Function MyFunction(TxtSource As String, TxtDestination As String, TxtTxtFind As String, TxtReplace As String)
 Dim TmpBuffer As String 'the buffer which will hold the input/output
 Dim BFreeFile As Integer 'this will hold the free file port

BFreeFile = FreeFile
Open TxtSource For Binary Access Read As #BFreeFile
TmpBuffer = InputB(2, BFreeFile) 'To Ignore the first character "?"
TmpBuffer = InputB(LOF(BFreeFile), BFreeFile) 'put all the contents to the TmpBuffer
Close #BFreeFile

TmpBuffer = Replace(TmpBuffer, TxtFind, TxtReplace, 1, -1, vbBinaryCompare) ' do the search/replace trick

BFreeFile = FreeFile
If Dir(TxtDestination) <> "" Then Kill (TxtDestination) 'to ensure that its written
Open TxtDestination For Binary Access Write As #BFreeFile
Put #BFreeFile, , TmpBuffer 'Output the file
Close #BFreeFile

End Function
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
OHDev2004Commented:
check the URL
hey fuzzyfluid what's the problem with this one this time :)
what's the error ,, did you try the above code ?
0
 
fuzzyfluidAuthor Commented:
if the link doesn't work, i'll post it up on the permanent server tomorrow. Thanks for all the quick responses. I was hoping i could get this done tonight but i guess it'll wait till tomorrow. OHDEV, i'll try your code in the morning. its 4 AM in NYC. Thanks.
0
 
OHDev2004Commented:
lol :)
0
 
fuzzyfluidAuthor Commented:
ok, heres the input file, FEENG.LOC

it's compressed so that bandwith doesnt get close to maxing out. The input file is original 400k.

www.kawaii-girls-want-to-s3x0r.us/revolutionary/FEENG.rar


any help would be appreciated.
0
 
OHDev2004Commented:
what's the problem this time,, :)

why does the file include weird characters at the beginning ???
did you output that file from vb or what ??
do  you want to do the same as the other one ?? or what  .....
OHDev
0
 
OHDev2004Commented:
it seams that you've put that jiberish at the beginning right ?
ok this time you try to find " Y a n k e e s" and replace it with "F u z k e e s"
then save it ,, am i right ?
if so the above function should work ,, i'll test it and let you know
0
 
OHDev2004Commented:
this really makes me crazy ,,
the jiberish at the beginning of the file makes it impossible to search and replace,,
donnow why ,, and when i try to truncate the jiberish; all the spaces in the words are removed ,, do you need that jiberish  at the beginning of the file ,, did you put it on purpose ? ,,,
OHDev
0
 
fuzzyfluidAuthor Commented:
no thats a totally different file. It's as is. The jibberish needs to be there. I tried out ur function on this file and it doesnt work. It removes the jibberish and puts ? marks.

I appreciate your hardwork and time into this. if you figure it out let me know.

1)the jibberish at the beginning is needed for the file to be interpreted properly.
2)i dont know what the jibberish is for, but if it is altered in any shape or form, it will not be read properly by the software.
0
 
OHDev2004Commented:
what software do you mean ?
i managed to output the file in the right order ,, but still cant search and replace
0
 
OHDev2004Commented:
you're welcome man :P
u can talk to me on msn msngr if you like ,,

0
 
fuzzyfluidAuthor Commented:
i figured it out.lol. since it's binary and the file isn't ascii. Those aren't spaces in between the letters.

Don't search "Y A N K E E S". Instead find

"Y" & vbnullchar & "A" & vbnullchar & "N" & vbnullchar & "K" & vbnullchar & "E" & vbnullchar & "E" & vbnullchar & "S"

and replace with
"F" & vbnullchar & "u" & vbnullchar & "z" & vbnullchar & "K" & vbnullchar & "E" & vbnullchar & "E" & vbnullchar & "S"


i'll award your the points because of the time you spent and the need function you posted that is helping me out. Thanks again ohdev.
0
 
OHDev2004Commented:
you know you really are very smart ,, really i mean it
its really a smart one !!
thanks but you know youre the one who must be awarded with those points ,, lol
0
 
OHDev2004Commented:
i will provide you with the latest functionally function now :) lol
if you like
0
 
OHDev2004Commented:
here is the final code man ,, hope it fullfills ur needs :)
 
=============================================

Private Sub Form_Load()
'its now written in the shape of a function so you can call
'it and specify different parameters if you like
'the parameters are :
'TxtSource --> the source file to read from
'TxtDestination --> the Destination file to write to
'TxtTxtFind--> the text to be searched for
'TxtReplace--> the text to be replaced
'Call MyFunction(App.Path & "\Zoobhrt.txt", App.Path & "\zyyy.txt", "N e w   Y o r k   Y a n k e e s", "N e w   Y o r k   T H I S W O R K S")
'MsgBox FileLen(App.Path & "\11.txt")
Call MyFunction(App.Path & "\FEENG.LOC", App.Path & "\FEENG2.txt", "Yankees", "Fuzkees")

End Sub


Private Function MyFunction(TxtSource As String, TxtDestination As String, TxtFind As String, TxtReplace As String)
 Dim TmpBuffer As String 'the buffer which will hold the input/output
 Dim BFreeFile As Integer 'this will hold the free file port

TxtFind = DecodeText2Bin(TxtFind)
TxtReplace = DecodeText2Bin(TxtReplace)

BFreeFile = FreeFile
Open TxtSource For Binary Access Read As #BFreeFile
'TmpBuffer = Input(1, BFreeFile) 'To Ignore the first character "?"
TmpBuffer = Input(LOF(BFreeFile), BFreeFile)  'put all the contents to the TmpBuffer
Close #BFreeFile


TmpBuffer = Replace(TmpBuffer, TxtFind, TxtReplace, 1, -1, vbBinaryCompare) ' do the search/replace trick
BFreeFile = FreeFile
If Dir(TxtDestination) <> "" Then Kill (TxtDestination) 'to ensure that its written
Open TxtDestination For Binary Access Write As #BFreeFile
Put #BFreeFile, , TmpBuffer 'Output the file
Close #BFreeFile
Unload Me
End Function



Private Function DecodeText2Bin(DTxt As String) As String
Dim x As Integer
Dim Tmp As String
For x = 1 To Len(DTxt) - 1
Tmp = Tmp & Mid(DTxt, x, 1) & vbNullChar
Next x
DecodeText2Bin = Tmp
End Function

0
 
fuzzyfluidAuthor Commented:
Thanks a billion Ohdev. And it's been a pleasure.

fuzZ
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now