I wrote this small program which reads every 5th character from Random.txt In random.txt I have one line of text: ABCDEFGHIJKLMNOPRST. I got the expected result:
- Position of A is 0
- Position of F is 5
- Position of K is 10
- Position of P is 15
Here is the code:
static void Main(string[] args)
{
StreamReader fp;
int n;
fp = new StreamReader("d:\\RANDOM.txt");
long previousBSposition = fp.BaseStream.Position;
//In this point BaseStream.Position is 0, as expected
n = 0;
while (!fp.EndOfStream)
{
//After !fp.EndOfStream were executed, BaseStream.Position is changed to 19,
//so I have to reset it to a previous position :S
fp.BaseStream.Seek(previousBSposition, SeekOrigin.Begin);
Console.WriteLine("Position of " + Convert.ToChar(fp.Read()) + " is " + fp.BaseStream.Position);
n = n + 5;
fp.DiscardBufferedData();
fp.BaseStream.Seek(n, SeekOrigin.Begin);
previousBSposition = fp.BaseStream.Position;
}
}
My question is, why after line while (!fp.EndOfStream)
BaseStream.Position
is changed to 19, e.g. end of a BaseStream
. I expected, obviously wrong, that BaseStream.Position
will stay the same when I call EndOfStream
check?
Thanks.