Read from stream

C

csharpula csharp

Hello,
I would like to know how can I read a file by this way:
Read from line X to Y and in other iteration from line Y to P and on and
on. How to implement it? Which method and how? Thank u!
 
J

Jon Skeet [C# MVP]

I would like to know how can I read a file by this way:
Read from line X to Y and in other iteration from line Y to P and on and
on. How to implement it? Which method and how? Thank u!

It's not clear what you mean - what are X, Y and P? Could you describe
the issue you're trying to solve in real-world terms?

Jon
 
M

Marc Gravell

If you are talking about "lines", then you probably want a
StreamReader, via File.OpenText(path) - which has methods such as
ReadLine(). If you are just reading forwards, then this should be
quite easy - just keep track of which line you are on. You could
probably wrap it up a bit for convenience, but it is hard to say "how"
without a bit more information on what you are trying to do...

Marc
 
C

csharpula csharp

I will try to explain myself :I want to read couple of times same file
which is being updated all the time. I want to read from beggining till
end at phase A and in phaseB to read from the end of A till the new end
and so on.
How to do it? Hope it's more clear. Thanks!
 
J

Jon Skeet [C# MVP]

csharpula csharp said:
I will try to explain myself :I want to read couple of times same file
which is being updated all the time. I want to read from beggining till
end at phase A and in phaseB to read from the end of A till the new end
and so on.
How to do it? Hope it's more clear. Thanks!

Well, you can find the position of a stream, then reopen the stream and
seek directly to that position, if that helps.
 
F

Family Tree Mike

Jon Skeet said:
Well, you can find the position of a stream, then reopen the stream and
seek directly to that position, if that helps.

.... and hope that the buffer is flushed on complete lines by the process
that is writing the file. It could get tricky otherwise.
 
C

christery

... and hope that the buffer is flushed on complete lines by the process
that is writing the file. It could get tricky otherwise.

and that one can read a file opend by another process... got that
problem switching from os/2 file servers to nt4 back in the days... NT
ans OS/2 share was read only for the reader but the file was locked in
nt, not in os/2, and the program doing it was bought, some old
datalogger, AAC-2 I think...

havent tried that since then in win, but got the feeling somehow that
an app like word for example locks files "harder" than notepad... not
investigated that, so it might be a problem if he cant control the
source of the file... in vms u can do a backup/ignore=interlock but
the problem with flushing the buffer (as Jon said) is still there.

can you get around that with storing data in a DB instead? then you
can use triggers and other methods (Sp to insert data only stores new
data in other table, when you read it clean it) to tell when new data
arrives too...

thats just ideas... good luck..

//CY
 
A

Arne Vajhøj

csharpula said:
I will try to explain myself :I want to read couple of times same file
which is being updated all the time. I want to read from beggining till
end at phase A and in phaseB to read from the end of A till the new end
and so on.
How to do it?

Some combination of Open/Seek/Close.

Maybe the little hack attached below can get you started.

Arne

================================================

using System;
using System.IO;
using System.Threading;

namespace E
{
public class TailSniff
{
private string filename;
private long pos;
public TailSniff(string filename)
{
this.filename = filename;
pos = 0;
}
public StreamReader Read()
{
FileStream fs = new FileStream(filename, FileMode.Open,
FileAccess.Read);
long len = fs.Length;
fs.Seek(pos, SeekOrigin.Begin);
byte[] b = new byte[len - pos];
fs.Read(b, 0, b.Length);
fs.Close();
pos = len;
return new StreamReader(new MemoryStream(b));
}
}
public class MainClass
{
public static void Main(string[] args)
{
TailSniff ts = new TailSniff(@"C:\z.z");
for(;;)
{
StreamReader sr = ts.Read();
string line;
while((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
sr.Close();
Thread.Sleep(10);
}
}
}
}
 
C

csharpula csharp

Hello,
The problem is that I can't use Seek or strea.lenth in StreamReader. So
how can I do it? Thanks!
 
M

Marc Gravell

I can't use Seek or strea.lenth in StreamReader

You can seek the .BaseStream and call .DiscardBufferedData(), though

Marc
 
C

csharpula csharp

But this stream is reading stream and it throwsexception in debug if I
use Seek with Stream. Any other ideas? Thansk a lot!
 
R

RobertK

You might try something like this:

static FileStream FS;
static StreamReader SR;
static int CharsRead;
static char[] buff;

public static void GetText()
{
int count = 0;
if (CharsRead > 1) SR.BaseStream.Position = CharsRead - 1;
do
{
count = SR.ReadBlock(buff, 0, 2048);
if (count > 0)
{
CharsRead += count;
string st = new string(buff);
if (count < 2048) st = st.Substring(0, count);
Console.WriteLine(st);
}
} while (SR.Peek() != -1);
}

static void Main(string[] args)
{
buff = new char[2048];
FS = new FileStream("D:\\testtext.txt", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
SR = new StreamReader(FS);
GetText();
Console.WriteLine ("Type 'quit' and ENTER to quit");
while (Console.ReadLine() != "quit")
{
GetText();
Console.WriteLine("Type 'quit' and ENTER to quit");
}
SR.Close();
 

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