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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read row n to row x in a textfile? 3

Status
Not open for further replies.

Madz

Technical User
Jan 31, 2001
33
0
0
SE
I want to read a textfile from row n and put the rows in a
string variable. I would then read until row x.

the textfile look like this
d:\hello1.bat
d:\hello2.bat
d:\hello3.bat

I know how to use the Line input to take one row only, but how to start from row 3 in a file?

thanks in advance!
/mats


 
Currently I am working on parser in VB and VC
After I read a lot,I didn't find any methods or function or something else for what you request.

You should think about an workaround - like readind the first two line and ignoring them for some condition in the Line Input loop (Of course you have a reason for the lines you want to skip).At least this is what I do, when I need to skip some line.

Another workaround you can use is the RegExp Object from VBScript. To use it you must add VBScript RegExp library to References.(details about RegExp->MSDN)

Hope of being of some help, s-) Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...

 
Something similar to the following may help (you'll need to add a reference to the MS Scripting Runtime to your project):

Public Function ReadALine(strFile As String, RequestedLine As Long) As String
Dim fso As FileSystemObject
Dim AllLines() As String
Dim tsFile As TextStream

Set fso = New FileSystemObject

Set tsFile = fso_OpenTextFile(strFile)
AllLines = Split(tsFile.ReadAll, vbCrLf) ' read and split entire file

Set tsFile = Nothing
Set fso = Nothing

If UBound(AllLines) >= (RequestedLine - 1) Then
ReadALine = AllLines(RequestedLine - 1) 'assuming array bounds start at 0
Else
' You need to handle the fact that you have
' tried to read a line that does not exist here
ReadALine = "" 'default handling
End If
End Function
 
You could use a Random file type.ie:
Add a standard module to the project and place this in it:

Code:
'define "record"
Type MyType
  LName As String * 25
  FName As String * 15
  IDNum As Long
End Type

On a form, place 2 command buttons on the left, and a textbox to the right. Then add the following code:
Code:
Private Sub Command1_Click()


'Save some records
Dim MyFile As Integer
Dim ctr As Integer
Dim udtUser As MyType
MyFile = FreeFile
Open "C:/MyFile.txt" For Random As MyFile Len = Len(udtUser)
For ctr = 1 To 10
  With udtUser
    .LName = "Last: " & CStr(ctr)
    .FName = "First: " & CStr(ctr)
    .IDNum = ctr
  End With
  Put MyFile, , udtUser   ' Write record to file.
Next
Close
End Sub

Private Sub Command2_Click()
    'get some records
    Dim MyFile As Integer
    Dim ctr As Integer
    Dim udtUser As MyType
    Dim n As Integer 'start record #
    Dim x As Integer 'End record  #
    
    n = 3
    x = 7
    MyFile = FreeFile
    Open "C:/MyFile.txt" For Random As MyFile Len = Len(udtUser)
    For ctr = n To x
        Get MyFile, ctr, udtUser
        With udtUser
            Text1.Text = Text1.Text & .LName & ", " & .FName & " ID= " & CStr(.IDNum) & vbCrLf
        End With
    Next
    Close
End Sub

You can also edit a particular record if you want:
Put MyFile, 7, udtUser ' Writes over record 7 Tim

Remember the KISS principle:
Keep It Simple, Stupid!
 
Or a more intuitive way:
-----------------------------------------------------------
Dim InFile As Byte, InLine As String, MyStr As String
Dim i As Integer

InFile = FreeFile
Open "c:\MyFile.txt" For Input As #InFile
For i = 0 To n 'where n is first line that you want
Line Input #InFile, InLine
If EOF(InFile) Then GoTo ErrHndl 'the file is not that long
Next i
MyStr = InLine
For i = 0 To x - n 'where x is last line that you want
Line Input #InFile, InLine
MyStr = MyStr & InLine
If EOF(InFile) Then GoTo ErrHndl 'the file is not that long
Next i
'The lines are now in MyStr
-----------------------------------------------------------
 
Place the file in an array (see faq222-482) and then process the lines of text by refering to their position in the array.
VCA.gif

Alt255@Vorpalcom.Intranets.com​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top