BaseStream.Seek(0, SeekOrigin.Begin)

  • Thread starter Thread starter Harry J. Smith
  • Start date Start date
H

Harry J. Smith

In the code below the two lines marked with ????????????????? do not work
properly.

The line:

inCF.BaseStream.Seek(0, SeekOrigin.Begin); // rewind inCF to
beginning of the file ??????????????????????

does not change the character position inCF.charPos, and the line:

st = inCF.ReadLine(); // read in next line ?????????????????????????

after reading the last line of the text file, will read the first line of
the file again and never returns a null string.

What am I doing wrong?

Other code in the file:

using System;

using System.IO;

using System.Text;

using System.Windows.Forms;

StreamReader inCF = new StreamReader(fName);



public static bool GoToLabel(ref string prSt, ref string st, ref
StreamReader inCF) // do a GoTo statement

{

bool upTo;

string label1;

bool findLabel;

bool byFile = (inCF != null);

string st7 = GetField(ref st);

if ((st7 != "GOTO") && (st7 != "GOUPTO")) return false;

upTo = (st7 != "GOTO");

if (upTo)

{

DelNPBls(ref st, 6);

}

else

{

DelNPBls(ref st, 4);

}

label1 = GetField(ref st);

if (upTo) st = prSt;

LookLabel(ref st, label1, out findLabel);

if (findLabel) return true;

if (!byFile) return true;

if (upTo)

{

inCF.BaseStream.Seek(0, SeekOrigin.Begin); // rewind inCF to
beginning of the file ??????????????????????

}

do

{

st = inCF.ReadLine(); // read in next line ?????????????????????????

if (st == null) break;

DelTabs(ref st);

LookLabel(ref st, label1, out findLabel);

if (findLabel) return true;

MultiID.MuInterrupt();

}

while (!MultiID.muAbort);

return true;

}

-Harry
 
Harry said:
In the code below the two lines marked with ????????????????? do not work
properly.

The line:

inCF.BaseStream.Seek(0, SeekOrigin.Begin); // rewind inCF to
beginning of the file ??????????????????????

does not change the character position inCF.charPos, and the line:


Call inCF.DiscardBufferedData() after you perform the Seek() operation
so the stream reader does not get stale data from its buffer.
 
mikeb said:
Call inCF.DiscardBufferedData() after you perform the Seek() operation
so the stream reader does not get stale data from its buffer.

Thanks Mike, I changed the code to:

inCF.BaseStream.Seek(0, SeekOrigin.Begin); // rewind inCF to beginning of
the file

inCF.DiscardBufferedData(); // Call inCF.DiscardBufferedData() after you
perform the Seek() operation

// so the stream reader does not get stale data from its buffer.

// This from mikeb in news group
microsoft.public.dotnet.languages.csharp 5/14/2004

and it fixed both problems.

-Harry
 
Back
Top