Re: Reading a text file

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Björn Holmgren (bjohol_at_hotmail.com)
Date: 06/14/04


Date: Mon, 14 Jun 2004 16:37:47 +0200


"Brian" <bfordSPAMOFF@markem.com> wrote in message
news:#jVl8vhUEHA.3780@TK2MSFTNGP10.phx.gbl...
> I need to use VB6 to read in and parse through a text file one line at a
> time, I was hoping to use Line Input but that doesn't work becaue the text
> file only contains a line feed and no carriage returns.
>
> How can I read in 1 line at time with only the line feed to work with? do
I
> have to do this one char at a time, I hope not :(

You can't read it line by line if VB doesn't recognize the linefeed.

Try reading it all at once into a string variable and then splitting it into
individual elements based on the linefeed character.

Example (air code):

    Const TEXT_FILE = "C:\Temp\MyFile.TXT"

    Dim F As Long
    Dim sBuffer As String
    Dim sLines() As String
    Dim Index As Long

    F = FreeFile
    Open TEXT_FILE For Binary Access Read As #F
    sBuffer = String(FileLen(TEXT_FILE), " ")
    Get #F,,sBuffer
    Close #F

    sLines = Split(sBuffer, vbLF)
    For Index = LBound(sLines)
        ' Process each line here
        Debug.Print sLines(Index)
    Next

--
Björn Holmgren
Guide Konsult AB


Relevant Pages

  • Re: Move through DataGridView records
    ... then bind the Binding Source to your DataSource, ... Dim index as integer = _ ... ByVal stringValue as String) ...
    (microsoft.public.dotnet.languages.vb.controls)
  • Re: Reading a text file
    ... > Dim sBuffer As String ... > Dim sLines() As String ... > Dim Index As Long ...
    (microsoft.public.vb.general.discussion)
  • RE: How do I find last occurence of a character in a text string in Ex
    ... Dim text As String ... Dim var As Variant ... Dim index As Long ...
    (microsoft.public.excel.programming)
  • Re: Object definition and structure, with variables
    ... DIM Index as integer, text as String, something, counter as long ... in this statement the variable called 'something' will default to Variant. ... DIM Index as integer, text as String, something as Variant, counter as long ...
    (microsoft.public.excel.programming)
  • Search pattern
    ... Dim strfile As String ... Dim bAddressFound As Boolean ... Dim strCurrentChar As String ...
    (comp.databases.ms-access)