Reading in a file in binary format

Posted on 2004-11-17
Medium Priority
Last Modified: 2010-05-02
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
    Close #1
    Close #2
End Function

This code doesn't generate any output file. Help !
Question by:levinlll
LVL 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 12604361
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
        MsgBox fileName, vbCritical, "File not found"
    End If
End Sub
LVL 35

Expert Comment

ID: 12604520
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
 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

LVL 28

Expert Comment

ID: 12604535
Your code should work if you define the string like so:

Dim charstring As String * 1

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question