Results 1 to 10 of 10

Thread: [RESOLVED] Move to line

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Far East
    Posts
    231

    Resolved [RESOLVED] Move to line

    Friends -- a quick help

    I use Set ts = F.OpenAsTextStream(1, 0)
    Once i run ts.readline, the pointer move to next line

    Question: How to move back to first line.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Move to line

    open again
    or read all into an array
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Far East
    Posts
    231

    Re: Move to line

    I know "readline" again goes to first line but is there no goto or moveto line. Thanks for dropping your hint.

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Move to line

    Why do you want to go to the first line a second time? Like suggested, read the entire file into an array, and then 'query' the zeroth index of the array if you want to see the 'first line' again.
    Sam I am (as well as Confused at times).

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Move to line

    If you insist on using the bloated and slow FSO at least avoid magic numbers. Those values have predefined names, e.g.:

    Code:
    Set ts = F.OpenAsTextStream(ForReading, TristateFalse)
    TextStream doesn't offer any way to "rewind" an opened text file.

    I doubt that slurping entire files into memory makes sense very often.

    Just use VB's native I/O statements for more versatility, less bloat, and better performance. Then you can use the Seek statement to rewind an opened file.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Far East
    Posts
    231

    Re: Move to line

    Dear diletante
    That's a good lesson. Now HOW to use VB native IO any pointer..sorry for bugging on. Cheers

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Move to line

    Code:
        Dim F As Integer
        Dim Text As String
    
        F = FreeFile(0)
        Open "sample.txt" For Input As #F
        Line Input #F, Text
        Debug.Print Text
        Seek #F, 1
        Line Input #F, Text
        Debug.Print Text
        Close #F

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Far East
    Posts
    231

    Re: Move to line

    Much appreciate Have a good time ahead bye

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Move to line

    By the way, Seek is both a Sub and a Function.
    If you open a file and then do something like

    ptr = Seek(F)

    The ptr will be equal to 1.
    After you do a "Line Input #F, Text", per dilettante's example, and then do the "ptr = Seek(F)", ptr will be equal to the start of the next line.

    So, if you have a large file and you would like to jump quickly to any line in the file without reading it all into memory, you can take the time when you open the file, to loop and call the Seek function before each Line Input and save the "pointer" value to the start of each line in an array of Longs.

    If the file isn't going to change, you could save that array of longs to a binary file and the next time you won't have to spend the time doing the initial read to populate the index array. You just do one read to fill the array from the binary file, and you can use the Seek Sub to Seek to any line index you want to read, and then do the Line Input.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Far East
    Posts
    231

    Re: [RESOLVED] Move to line

    Thanks PASSEL .. another lesson to accumulate

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width