|
-
Nov 27th, 2002, 11:15 AM
#1
Thread Starter
Lively Member
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 i = 1 To UBound(aryList)
lst1.AddItem aryList(i)
Next
End Sub
Last edited by trutta; Nov 27th, 2002 at 07:28 PM.
-
Nov 27th, 2002, 12:45 PM
#2
Fanatic Member
Re: How to read one line at a time?
Mabe try replacing this with this ?
VB Code:
aryList = Split(strFile, vbCrLf)
-
Nov 27th, 2002, 01:03 PM
#3
Lively Member
Real simple
let me know if it works
Dave
-
Nov 27th, 2002, 01:29 PM
#4
Let me in ..
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
-
Nov 27th, 2002, 01:44 PM
#5
Thread Starter
Lively Member
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
-
Nov 27th, 2002, 03:14 PM
#6
Fanatic Member
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:
Private Sub FillList(lst1 As ListBox)
Dim aryList() As String, aryitem() As String
Dim i As Integer
aryList = Split(Input(LOF(1), 1), vbCrLf)
For i = 0 To UBound(aryList) - 1 'Loop through every line found
aryitem = Split(aryList(i), ",")
lst1.AddItem aryitem(0)
lst1.AddItem aryitem(1)
lst1.AddItem aryitem(2)
lst1.AddItem aryitem(3)
Next i
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?
-
Nov 27th, 2002, 04:55 PM
#7
Thread Starter
Lively Member
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.
-
Nov 27th, 2002, 07:27 PM
#8
Thread Starter
Lively Member
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 String, strFile As String
Dim i As Integer
Do Until EOF(1)
Line Input #1, strFile
aryList = Split(strFile, ",")
For i = 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|