Click to See Complete Forum and Search --> : Stream.Seek problems


gjs368
November 26th, 2002, 09:38 AM
I have a random-access text file that I need to be able to navigate through by a defined number of characters in order to pull out individual records. I am using Stream.Seek to position the file pointer to the proper place, and I can tell by checking Stream.Position that the pointer does indeed appear to move to the requested location. However, StreamReader.Read() always picks up the very next character after the last character read in the last iteration :confused:

Even stranger: I have some header information that I want to skip past (FILEHDR_STRLEN) and the buffer always gets the proper information on the first read. :confused: :confused:

Here is the code in question:
StreamReader srRead = new StreamReader((System.IO.Stream)File.OpenRead(strFilename), System.Text.Encoding.ASCII);
int nIter = 0;
char[] buffer =new char[10];
while (srRead.BaseStream.Position < srRead.BaseStream.Length)
{
srRead.BaseStream.Position = 0;
srRead.BaseStream.Seek((FILEHDR_STRLEN + (SITEINFO_STRLEN * nIter)), SeekOrigin.Begin);
srRead.Read(buffer, 0, 10);
nIter++;
}

srRead.DiscardBufferedData();
srRead.Close();

I have tried various forms of this block, but always get the same result.

Any Ideas?

gjs368
December 1st, 2002, 08:59 AM
Perhaps there aren't many C-Sharpies on this forum yet :( .

At any rate, I did find the cause of my problem; All I needed to do to get it to work was to flush the stream before the next call to Read(). :)

Thanks to any and all that took the time to read this thread, even if they did not feel qualified to give an answer.