How to manage backspace/backline

E

Ernst Sauer

Hello,

I want (for example) to read a certain line again,
so I thought this should work:

StreamReader sr;
sr = new StreamReader(rfile)
string line;
long pos;
int i=0;

while (i++<3)
{ if (i==2) pos=sr.BaseStream.Position;
line=sr.ReadLine();
sr.DiscardBufferedData(); // no success with and witout
}
sr.BaseStream.Seek(pos, SeekOrigin.Begin);


I have a small test-file.
With the first ReadLine() sr reads the whole file
(later he splits the lines)
and sets the pos to the end of the stream.
With sr.DiscardBufferedData() I can't even read the second line.

Of course I can write my own method, but is this really necessary?

Thanks
E.S.
 
A

Arne Vajhøj

I want (for example) to read a certain line again,
so I thought this should work:

StreamReader sr;
sr = new StreamReader(rfile)
string line;
long pos;
int i=0;

while (i++<3)
{ if (i==2) pos=sr.BaseStream.Position;
line=sr.ReadLine();
sr.DiscardBufferedData(); // no success with and witout
}
sr.BaseStream.Seek(pos, SeekOrigin.Begin);


I have a small test-file.
With the first ReadLine() sr reads the whole file
(later he splits the lines)
and sets the pos to the end of the stream.
With sr.DiscardBufferedData() I can't even read the second line.

Of course I can write my own method, but is this really necessary?

If it is just a few lines back, then I would just keep N lines
in memory (circular buffer) and use that data structure when going
back.

For arbitrary number of lines back I would probably implement
a class with a ReadLine myself.

I can not get DiscardBufferedData to work either.

Arne
 
A

Arne Vajhøj

Without a concise-but-complete code example (including example data) it's
hard to know for sure what you're trying to do and why it's not working for
you.

However, based on the code you did post, I will point out that retrieving
the current Position value of BaseStream is unlikely to produce the results
you want, because the stream is being read into a buffer. Just as you've
found, the current position just before a call to ReadLine() could in fact
be well past the point in the stream from which the data returned by
ReadLine() comes from.

The DiscardBufferedData() works fine, but you have to have an accurate
position value to use for the StreamReader to start reading data again.

That is most likely the cause.

So if:

pos=sr.BaseStream.Position;

is replaced with a:

pos += sr.CurrentEncoding.GetByteCount(line) + Environment.NewLine.Length;

after each ReadLine that should not be reread, then it may work.

Arne
 
E

Ernst Sauer

Am 09.09.2012 03:34, schrieb Peter Duniho:
..


First, you need to explain exactly what you're trying to do. Why do you
want to re-read a previously read line of the file?

The problem is very simple, I read blocks of data, like:

POINT
x1 y1 z1

xn yn zn

LINE
lp1 rp1
….
lpn rpn

The end of a block is indicated by the name of the new block. To know
the end I must read the first line of the new block. For several reasons
( the blocks may be located in different files) I always want to put the
reading-pointer to the beginning of the new block. Here a BACKSPACE
command would be the best (like in the (not good) old FORTRAN-days).
But I see, I have to write my own method or workaround.

Ernst
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top