Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting certain lines from Text file

Status
Not open for further replies.

JonathonC

Technical User
Aug 18, 2001
43
GB
Ok, i've go this piece of code to get a certain line from a text file.

Dim LinesFromFile as new collection
Open FilePath For Input As #1
Do While Not EOF(1)
Line Input #1, MyString
LinesFromFile.add MyString
Loop
Close #1 ' Close file.
MsgBox "FirstLine=" & LinesFromFile(1)

How could I get the code to get all the lines from a text file apart from line 1 and 2?

Thanks,

Jonathon.
 
Dear Jonathon,


the quick and dirty solution would be:

Dim LinesFromFile as new collection
Open FilePath For Input As #1
Line Input #1, MyString
Line Input #1, MyString
'after the above 2 lines are read and not yet used.
Do While Not EOF(1)
Line Input #1, MyString
LinesFromFile.add MyString
Loop
Close #1 ' Close file.
MsgBox "FirstLine=" & LinesFromFile(1)

you could also delete (clear) 2 times the first object in your collection.

HTH
regards Astrid
 
Correct me if i am wrong , but you already have loaded all of the lines from the file.
Just that you only look at the first line of the file in the msgbox because thats what you told it to do. try changing the number in the ( ) to three and see what you get.

MsgBox "FirstLine=" & LinesFromFile(3)
 
Putting a 3 in the brackets gives just line 3, but I want line 3 and beyond.

Jonathon.
 
Try this... read the entire file into an array in one operation and then populate LinesFromFile starting with whatever line you want to start with (only works with VB6).
Code:
Open FilePath For Binary As #1
G$ = String$(LOF(1), 0)
Get #1, 1, G$
MyArray = Split(G$, vbNewLine)
Close #1
StartWithLine = 3
For Rep = 0 To UBound(MyArray)
    If Rep >= StartWithLine - 1 Then
        LinesFromFile.add MyArray(Rep)
    End If
Next
This is quite a bit faster than the sequential file action you posted.
VCA.gif
 
Darn, I'm using VB5, anyone got any other suggestions?

Jonathon.
 
You can do the same thing. You just can't use the Split function.

Dimension MyArray at the module level.
[tt]Dim MyArray(0) As String[/tt]

Then...
[tt]
ReDim MyArray(0 To 0) As String
Open FilePath For Binary As #1
G$ = String$(LOF(1), 0)
Get #1, 1, G$
Close #1
StartWithLine = 3
Do
EndLine = InStr(G$, vbNewLine)
If EndLine > 1 Then
Temp$ = Left$(G$, EndLine - 1)
MyArray(UBound(MyArray)) = Temp$
ReDim Preserve MyArray(0 To UBound(MyArray) + 1) _
As String
G$ = Right$(G$, Len(G$) - (Len(Temp$) + 2))
Else
Exit Do
End If
Loop

For Rep = 0 To UBound(MyArray) - 1
If Rep >= StartWithLine - 1 Then
List1.AddItem MyArray(Rep)
End If
Next
[/tt]
VCA.gif
 
Doing that just seams to get the second line from the bottom!

Jonathon.
 
This probaly wont work but how about doing something like:

Lines = LinesFromFile(LinesFromFile.Count)
LinesNext = Lines - 2

then open 'LinesNext' lines from the bottom all the way down to 'Lines'.


Jonathon.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top