Random.txtから5文字ごとに読み取るこの小さなプログラムを作成しました。random.txtには、ABCDEFGHIJKLMNOPRSTという1行のテキストがあります。期待どおりの結果が得られました。
- Aの位置は0です
- Fの位置は5です
- Kの位置は10です
- Pの位置は15です
コードは次のとおりです。
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;
}
}
私の質問は、なぜ行while (!fp.EndOfStream)
BaseStream.Position
が19に変更されたのか、たとえばの終わりですBaseStream
。BaseStream.Position
私がチェックを呼び出すとき、それは明らかに同じままであると私は予想しました、明らかに間違っていますEndOfStream
か?
ありがとう。