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!

Moving To Specific Line # in an Input Text File

Status
Not open for further replies.

jstiegelmeyer

Programmer
Dec 31, 2002
27
US
Quick overview of question:
If I open a text file with the Open Statement in Sequential Mode, is there any way to jump to a specific line number?

----------------------------------------------------------

I know that it is possible to jump to a specific byte, or char position using the Seek Statment, but is there a way to jump to a specific line?

Here's how I open the file:

Open "MyFile.txt" For Input As #1

I'd like to be able to move to Line #3.

Does anyone know of a good way to do this?

MyFile.txt:
This is line #1
This is line #2
This is line #3. I want to display this line.
This is line #4.
This is line #5.
 

Well if your lines are all the same number of characters you could use the seek function to jump to that line. If not you could read in the entire file into a collection/dictionary/array and use the index to display the line as needed.

Good Luck

 
Ouch. My lines are not all the same size.

I was hoping there was an easier way than to load up an array.

But...if that's all there is, that's what I'll do. Thanks for the help!
 

No, you really don't have to do that. You could just call a get line # function that first seek to the beginning of the file. Then skips lines 1 through 1 less than line wanted and then returns line wanted. If you use this method I would then put some checks in for EOF in the skip part and right before the return part.

Good Luck

 
That is a good idea, but it could be slow to read through the file every time.

The file I'm using has approximately 3,000 lines.
 

If you are reading it in squentially then on the next button read in and display the next line and add it to the array for moving previous.

Good Luck

 
Don't use Sequential Mode if your worried about processing time for lots of records.

Use Random access. You can jump to any record in the file..so the amount of records won't matter.

 
From what I understand about Random Access, I can't use it because the records are a fixed length, but my lines are all different lengths.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top