Reading in a file in binary format

I'm trying to read in a file in binary format so that I can replace the LF that are in the file with CRLF. Here's the code I am using:

Dim ReformatFileName As String
Dim inFileCharCount As Long
Dim outFileCharCount As Long
Dim charstring As String

        ' open passed file for read
    Open TxtLocation For Binary As #1
   
        ' create new file based on passed file
    ReformatFileName = TxtLocation & ".fmt"
        ' and open it for writing
    Open ReformatFileName For Binary As #2
   
    inFileCharCount = 1
    outFileCharCount = 1
    Do While Not EOF(1)
            ' get a character from the input file
        Get #1, inFileCharCount, charstring
       
        inFileCharCount = inFileCharCount + 1
       
            ' if we come to a LF, put a CR in front of it
        If charstring = vbLf Then
            Put #2, outFileCharCount, vbCr
            outFileCharCount = outFileCharCount + 1
        End If
       
            ' and write it out to the output file
        Put #2, outFileCharCount, charstring
        outFileCharCount = outFileCharCount + 1
       
       
    Loop
    Close #1
    Close #2
   
End Function

This code doesn't generate any output file. Help !
levinlllAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
How about like this instead:

Option Explicit

Private Sub Command1_Click()
    replaceInFile "C:\someFile.txt", vbLf, vbCrLf
End Sub

Private Sub replaceInFile(ByVal fileName As String, ByVal oldString As String, ByVal newString As String)
    Dim ff As Integer
    Dim entireFile As String
   
    If Dir(fileName) <> "" Then
        ff = FreeFile
        Open fileName For Binary Access Read As #ff
        entireFile = Input(LOF(ff), ff)
        Close #ff
   
        entireFile = Replace(entireFile, oldString, newString)
   
        ff = FreeFile
        Open fileName For Binary Access Write As #ff
        Put #ff, , entireFile
        Close #ff
    Else
        MsgBox fileName, vbCritical, "File not found"
    End If
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mvidasCommented:
Hi levinlll

Here is one that uses regular expressions to make sure that the LF you're replacing doesn't already have a CR before it.  Just an alternative if you haven't already solved this.

Function ReplaceLFwithCRLF(TxtLocation As String) As Boolean
 Dim vFileNumIn       As Integer
 Dim vFileNumOut      As String
 Dim ReformatFileName As String
 Dim charstring       As String
 Dim RegEx            As Object
 On Error GoTo ErrChk
 Set RegEx = CreateObject("vbscript.regexp")
 RegEx.Global = True
 RegEx.Pattern = "[^\013]\010"
 ReformatFileName = TxtLocation & ".fmt"
 vFileNumIn = FreeFile()
 Open TxtLocation For Binary As #vFileNumIn
 vFileNumOut = FreeFile()
 Open ReformatFileName For Binary As #vFileNumOut
 charstring = Input(LOF(vFileNumIn), #vFileNumIn)
 Close #vFileNumIn
 charstring = RegEx.Replace(charstring, vbCrLf)
 Put #vFileNumOut, , charstring
 Close #vFileNumOut
 Set RegEx = Nothing
 ReplaceLFwithCRLF = True
 Exit Function
ErrChk:
 Set RegEx = Nothing
 Close #vFileNumIn
 Close #vFileNumOut
 ReplaceLFwithCRLF = False
End Function

Sub testit()
 If Not ReplaceLFwithCRLF("C:\filename") Then Msgbox "An error occurred"
End Sub


Matt
0
vinnyd79Commented:
Your code should work if you define the string like so:

Dim charstring As String * 1
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.