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

Inputing text from a text file

Status
Not open for further replies.

lmb3

Programmer
Jul 10, 2001
16
US
I know how to input a line of text from a text file in a sequential order using:

Line Input #filenum, varName

I am curious about whether there is a way in VB to if you know that there are for example, 14 lines in your text file, to input Line #7 without first inputting lines 1-6. I hope that makes sense. I want to just be able to input a specific line and only that line from a text file. Any ideas on how to do this efficiently without just "scrolling" through the file and inputting line after line until I get the one I want.

Thanks in advance!
 
I think what you want is an indexed file, but I don't think this is possible with DOS. You'd have to use another program like Access to create a database, and then use a data access tool like ODBC or Jet to get at the data in an indexed fashion.
 
If you know how long each line is every time, you can open the file as RANDOM instead of INPUT. But you will have to assign the record length to xxx first. Instead of using
Code:
Input #1, var[\code]
you would use:
[code]Get #1, 7, var[\code]

Again, though, you have to know how long each line is to do that.

Kevin
 
if you use the File System Object you can accomplish this w/out any trouble at all.
Code:
'Instantiating the object variable mFileSysObj
    Set mFileSysObj = New Scripting.FileSystemObject
    ' Get the file
    Set mFile = mFileSysObj.GetFile(App.Path & "\ConstellationPRG.txt")

    ' Open a text stream for reading from a file
    Set mTxtStream = mFile.OpenAsTextStream(ForReading)
    'Set objTemp =
    
    'reading data from file as long as it is not the end of file
    While Not mTxtStream.AtEndOfStream

        ' Read the data
        If (mTxtStream.Line =7) Then
            strData = mTxtStream.ReadLine
        End If
    Wend

    Call mTxtStream.Close
 
But isn't this FileSystemObject method the same as "just 'scrolling' through the file and inputting line after line until I get the one I want."? Seems like it's still reading through the file until it gets to the 7th line, instead of jumping directly to the 7th record, as it would in an indexed file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top