Results 1 to 8 of 8

Thread: How to read one line at a time?[Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Location
    Michigan
    Posts
    107

    How to read one line at a time?[Resolved]

    I'm reading a multiline, comma separated file into an array by splitting the elements by the comma delimiter, and using that to populate a listbox. But I also get the end of line binary character, which I don't want. How can I read one line at a time? I can do it in embedded VB, but don't find the same method on VB6. Thanks.
    PHP Code:
    Private Sub FillList()
      
    Dim aryList() As String
      Dim i 
    As Integer
      strFile 
    Input(LOF(1), 1)
      
    aryList Split(strFile)
      For 
    1 To UBound(aryList)
        
    lst1.AddItem aryList(i)
      
    Next
    End Sub 
    Last edited by trutta; Nov 27th, 2002 at 07:28 PM.

  2. #2
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    Question Re: How to read one line at a time?

    Mabe try replacing this
    VB Code:
    1. aryList = Split(strFile)
    with this ?
    VB Code:
    1. aryList = Split(strFile, vbCrLf)
    Give your music collection a whole new life with PartyTime Jukebox

  3. #3
    Lively Member
    Join Date
    Nov 2002
    Location
    Delaware
    Posts
    126
    Real simple

    VB Code:
    1. line input #1, strFile

    let me know if it works

    Dave

  4. #4
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    Dim hFile
    Dim strFile

    hFile = FreeFile

    Open "c:\test.txt" For Input As #hFile

    While Not EOF(hFile)
    Line Input #hFile, strFile

    Wend
    close #hFile

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Location
    Michigan
    Posts
    107
    Thanks for the replies, but neither worked for me. I can't use vbCrLf as the delimiter in Split because each line has 4 elements, separated by 3 commas, and I need to get each element, e.g.
    a, b, c, d <CrLf>
    strFile is a module variable, and the file is opened via common Dialog in another sub.
    Using Line Input... didn't give any errors, but didn't put anything in the listbox either:
    PHP Code:
    Private Sub FillList()
      
    Dim aryList() As String
      Dim i 
    As Integer
      
    'strFile = Input(LOF(1), 1)
      Line Input #1, strFile
      aryList = Split(strFile)
      For i = 1 To UBound(aryList)
        lst1.AddItem aryList(i)
      Next
    End Sub 

  6. #6
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    Question

    I don't see in your code where you're setting your comma delimitter but if as you state, each line always has four items separated by 3 commas, maybe you could try this...
    VB Code:
    1. Private Sub FillList(lst1 As ListBox)
    2.   Dim aryList() As String, aryitem() As String
    3.   Dim i As Integer
    4.      aryList = Split(Input(LOF(1), 1), vbCrLf)
    5.  
    6.   For i = 0 To UBound(aryList) - 1 'Loop through every line found
    7.       aryitem = Split(aryList(i), ",")
    8.         lst1.AddItem aryitem(0)
    9.         lst1.AddItem aryitem(1)
    10.         lst1.AddItem aryitem(2)
    11.         lst1.AddItem aryitem(3)
    12.   Next i
    13. End Sub

    If your file looks something like below, this would load your ListBox with 8 individual entries:

    First item,Second item,Third item,Fourth item
    Fifth item,Sixth item,Seventh item,Eighth item

    Is this what your looking for?
    Give your music collection a whole new life with PartyTime Jukebox

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Location
    Michigan
    Posts
    107
    Thanks, daydee, but not that either. See my first post for the comma delimiter, the second was an attempt to incorporate Dave Keatley's suggestion, although maybe I did it incorrectly. Also, I don't want to be bound by only four entries per line; that just happens to be what's in the test file.
    Also, doesn't your split function have too many arguments?
    In eVB, the code might go like this, albeit for a listview, not listbox:
    PHP Code:
    File1.Close     ' Make sure file is closed first
        lvStock.ListItems.Clear     ' 
    Empty listview
        i 
    1
        File1
    .Open "\My Documents\" & cboPONum.Text, fsModeInput, fsAccessRead
        ' Get data from file and populate listview
        Do Until File1.EOF = True
            myString1 = File1.LineInputString   ' Get a line from the file
            If cboPONum.Text = "
    test.txt" Or cboPONum.Text = "alltest.txt" Then
                myString2 = Split(myString1, "
    ,")   ' separate the fields
            Else
                myString2 = Split(myString1, Chr(&HBC))
            End If
            Set thing = lvStock.ListItems.Add(i, CStr(myString2(0)), myString2(0))  ' Fill listview
            For j = 1 To 1  ' This allows more items to be auto added if needed
                thing.SubItems(j) = myString2(j)
            Next
            i = i + 1
        Loop
        File1.Close 
    The file opening is a little different in eVB, but basically I'm looking for something like "LineInputString". Given a file like:
    a, b, c, d
    e, f g, h
    I'd like the listbox to display:
    a
    b
    c
    .
    .
    h
    Probably just dumb and missing something obvious, but that would be nothing new.
    Last edited by trutta; Nov 27th, 2002 at 04:59 PM.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Location
    Michigan
    Posts
    107
    Thanks for all your help, you put me on the right track. The working code is posted below. I'm more used to working in Delphi & eVB for work, and forget/DNK VB functions.
    PHP Code:

    Private Sub FillList()
      
    Dim aryList() As StringstrFile As String
      Dim i 
    As Integer
      
    Do Until EOF(1)
        
    Line Input #1, strFile
        
    aryList Split(strFile",")
        For 
    0 To UBound(aryList)
          
    lst1.AddItem aryList(i)
        
    Next
      Loop
    End Sub 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width