reading backwards with the seek, peek, and position functions 
Author Message
 reading backwards with the seek, peek, and position functions

I am trying to read a flat file from the end so that I don't have to read
through the entire file every time I access it (the file will continue to
grow).  I would like to read the beginning of each line (line length 320)
starting at the end, until I find a line beginning with (for example) a "Y".
I am using a filestream to open the file and a streamreader to read.  I
cannot get any combination of the seek, peak, and position functions of the
filestream and streamreader objects to work the way that I want.  It seems
that after I use streamreader.peek, the current position of the stream goes
back to where it was before I used the filestream.seek.  I have also tried
setting the position directly rather than with seek, but with the same
results.  Thanks a lot for any help.

Justin.

fs = new filestream
fsinstream = new streamreader(fs)
fsinstream.basestream.seek(0, seekorigin.end)
do
    fsinstream.basestream.seek(-322, seekorigin.current)
loop while chrw(fsinstream.peek) <> "Y"



Sun, 01 May 2005 06:46:13 GMT  
 reading backwards with the seek, peek, and position functions
I did this without using the StreamReader, just using the base FileStream
object.

My Test File looked like (each line 12 chars long including the CRLF)
AAAAAAAAAA
YAAAAAAAAA
AAAAAAAAAA

Sub Test()
        Dim fs As New IO.FileStream("C:\TEST.TXT", IO.FileMode.Open)
        Dim b(0) As Byte

        ' Move to end of file
        fs.Position = fs.Length

        ' Move back by 1 line
        fs.Position -= 12


        fs.Read(b, 0, 1)
        fs.Position -= 1

        Do Until b(0) = 89
            ' Move back by 1 line.
            fs.Position -= 12


            fs.Read(b, 0, 1)
            fs.Position -= 1
        Loop
        fs.Close()
End Sub

Thanks
Gary Spangler [MS]
--
This posting is provided "AS IS" with no warranties, and confers no rights.

Quote:
> I am trying to read a flat file from the end so that I don't have to read
> through the entire file every time I access it (the file will continue to
> grow).  I would like to read the beginning of each line (line length 320)
> starting at the end, until I find a line beginning with (for example) a
"Y".
> I am using a filestream to open the file and a streamreader to read.  I
> cannot get any combination of the seek, peak, and position functions of
the
> filestream and streamreader objects to work the way that I want.  It seems
> that after I use streamreader.peek, the current position of the stream
goes
> back to where it was before I used the filestream.seek.  I have also tried
> setting the position directly rather than with seek, but with the same
> results.  Thanks a lot for any help.

> Justin.

> fs = new filestream
> fsinstream = new streamreader(fs)
> fsinstream.basestream.seek(0, seekorigin.end)
> do
>     fsinstream.basestream.seek(-322, seekorigin.current)
> loop while chrw(fsinstream.peek) <> "Y"



Tue, 03 May 2005 05:23:05 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Seeking advice on searching a string backwards

2. Seeking backwards in AVI file

3. Reading a file backwards

4. HELP: functions inp/out/peek/poke for working with AD/DA board in VB4

5. seeking and record position

6. 3-Yr VB Programmer Seeks Position

7. Part Time Positions Sought

8. 3-Yr VB Programmer Seeks Position

9. Position Sought

10. Seeking for VB programming position

11. 2:1 graduate, seeks perm position

12. Linking Data1 scroll position with seek command result

 

 
Powered by phpBB® Forum Software