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

Reset cursor to first line in TEXT file

Status
Not open for further replies.

ChewDoggie

Programmer
Mar 14, 2005
604
US
Hello All !

I have a text file that is opened via this command:

Code:
Open cdlOpen.FileName For Input As #1

and I want to (programatically) read a couple of lines in the file and then reset the cursor to the first line in the file.

I've been able to read each line but does anyone know how to reset the cursor to the first line in the file again?

Thanks!




AMACycle

American Motorcyclist Association
 
You can't, without closing and re-opening the file. That's the nature of sequential file access.

Look into random access files or simple databases.
 
If you use the file system object to read all the data, then split it in to an array, all of the data will be accessible.

Something like this...

Code:
Dim FSO As Scripting.FileSystemObject
Dim Data As String
Dim arData() As String
Dim i As Long

Set FSO = CreateObject("Scripting.FileSystemObject")
Data = FSO.OpenTextFile("[!]C:\filename.ext[/!]", ForReading).ReadAll
arData = Split(Data, vbCrLf)

For i = LBound(arData) To UBound(arData)
    Debug.Print arData(i)
Next

This will put the entire file in to an array that you can easily manipulate any way you want.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
KISS and try

Seek 1,1

The following is in vb6 help
<<
Seek Statement


Sets the position for the next read/write operation within a file opened using the Open statement.

Syntax

Seek [#]filenumber, position

The Seek statement syntax has these parts:

Part Description
filenumber Required. Any validfile number.
position Required. Number in the range 1 – 2,147,483,647, inclusive, that indicates where the next read/write operation should occur.


Remarks

Record numbers specified in Get and Put statements override file positioning performed by Seek.

Performing a file-write operation after a Seek operation beyond the end of a file extends the file. If you attempt a Seek operation to a negative or zero position, an error occurs.
>>

HTH Hugh
 
Don't forget that Seek is primarily designed for Random access files. You can use it on Input files, but you only get the byte position, not the record position. In this example it will work, as you only want the start, but if you want to Seek other positions you will need to keep track of number of bytes.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top