Reading in a file in binary format

Posted on 2004-11-17
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 85

    Accepted Solution

    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

    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

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

    Dim charstring As String * 1

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now