Link to home
Start Free TrialLog in
Avatar of cc_2000_sg
cc_2000_sg

asked on

how to access specific line of a file

hi,

i have a file with  more than 10000 lines.

and i want to access specific line of the file,

for example, i want to get the data of 8000th line, and i do not want to read the first 7999 line of data,

so how to do that?


thanks

cc
Avatar of dmarco
dmarco
Flag of Italy image

You can do it, if your file is a random access file:

Open "myfile" For Random As #Filenumber Len = Len (MyRecord)
Get #Filenumber, 8000, MyVar  ' read the 8000th record in your file and put it into the variable MyVar
Close #Filenumber

If your file isn't a Random Access, you cannot read the 8000th line (text line I suppose) without reading the previous 7999.

Hope this helps

Marco
ASKER CERTIFIED SOLUTION
Avatar of Enlade
Enlade
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The above proposed solutions are good tricks to speed up the reading of a sequential file, but they cannot avoid to read first the entire file.

As could be said by Mr De Lapalisse : the only way to access randomly a file is to have a Random file saved in your disk... :-))

However, it would be interesting to know  what kind of file we are talking about .....

cc_2000_sg, can you give us some more info ?

Again, you don't need to open the file for random access, but you do need the file to be formated with the same restrictions that are required for a Random Access method (see my comment above).

In any case, if you want line 2 of a 100,000 line file then the above approuches will read all 100,000 lines.  It might be better to just read each line up to the line you want and then stop.  That way you don't have to deal with such a large memory allocation.  For instance, something like this:


Private Function GetLine(ByVal FFN As String, ByVal LineNum As Long) As String
  Dim ret As String
  Dim i As Long
 
  Open FFN For Input As 1
 
  i = 1
  While Not (EOF(1)) And (i <= LineNum)
    Line Input #1, ret
    i = i + 1
  Wend
   
  If ((i - 1) = LineNum) Then
    GetLine = ret
  Else
    GetLine = ""
  End If
 
  Close #1

End Function


Mind you if we can make some assumptions about the input data then you might be able to jump directly to the line that you want to read (see my previous note for conditions).

cc_2000_sg, did we help you with this problem?  Did you resolve this problem?

cc_2000_sg, if you got the answer you needed could you assign points so we can close this issue?  Thanks.

cc_2000_sg, if you got the help you needed then can you assign points for this question?  Otherwise, could you clarify your question a bit more so that we can finish helping you with this question?  Thanks.

Again, could you please close out this question?  If someone helped you please assign the points.  Otherwise, please clarify what more help we could be to you.  Thanks.
Avatar of DanRollins
Moderator, my recommended disposition is:

    Split points between: Enlade and zzzzzooc and MichaelDS and supunr

DanRollins -- EE database cleanup volunteer