Reading a file as long as it is open by another process

U

Udi

Hi,
I have two processes, one writres a text file and the other reads it.
I'm looking for a way to pend the reader when it reaches to the end of
the file, for as long as the writer holds the file open.
i.e. if the reader reaches EOF, and then the writer adds a new row,
i'd like the reader to be able to read the new rows until the writer
releases the file.

Is there an easy way to implement this?
Thanks,
Udi
 
R

RayLopez99

Hi,
I have two processes, one writres a text file and the other reads it.
I'm looking for a way to pend the reader when it reaches to the end of
the file, for as long as the writer holds the file open.
i.e. if the reader reaches EOF, and then the writer adds a new row,
i'd like the reader to be able to read the new rows until the writer
releases the file.

Is there an easy way to implement this?
Thanks,
Udi

Not sure what you mean--you mean 'read only'? I think that's doable
even if you have several processes reading the same stream. Try it
and see.

RL
 
A

Arne Vajhøj

I have two processes, one writres a text file and the other reads it.
I'm looking for a way to pend the reader when it reaches to the end of
the file, for as long as the writer holds the file open.
i.e. if the reader reaches EOF, and then the writer adds a new row,
i'd like the reader to be able to read the new rows until the writer
releases the file.

If the file meta data is kept uptodate, then you may be able to
do it by maintaining position and compare that with file length.

In general that write-file-read-file approach is error-prone. Wny
not use named pipe/TCP socket instead? Much easier!

Arne
 
B

Bert Hyman

In
Udi said:
i'd like the reader to be able to read the new rows until the writer
releases the file.

You could try re-opening the file with OF_SHARE_EXCLUSIVE or
OF_SHARE_DENY_WRITE. I'm sorry that I can't name the specific C#
class/method for doing that, but it must be in there somewhere :)

So long as any other process has the file open for writing, your attempt
will fail. Once you're able to open the file in either of those modes,
you'll know that the writing process has closed the file.

That would involve closing and opening the file each time you check, but
that might not be too much trouble.
 
U

Udi

Thanks all, I appreciate the help.

The file is a given fact. It is created and written by a third party
application.
So unless there's a way to treat the file as a named pipe,
i guess i'm stuck with saving the position between the calls?

Udi
 
U

Udi

BTW, waiting for the writing process to end is not an option since i
need to start parsig the file as soon as it is created.
(it is kind of a log file)
Thanks again,
Udi
 
P

Peter Duniho

Udi said:
Thanks all, I appreciate the help.

The file is a given fact. It is created and written by a third party
application.
So unless there's a way to treat the file as a named pipe,
i guess i'm stuck with saving the position between the calls?

As Arne says, you could look at the "modified" time stamp for the file.

Also, the FileSystemWatcher class may help you monitor changes to the
file, to know when to try to read more data from it.

Pete
 
A

Arne Vajhøj

The file is a given fact. It is created and written by a third party
application.
So unless there's a way to treat the file as a named pipe,
i guess i'm stuck with saving the position between the calls?

I think so.

See below for some code to 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, FileShare.Write);
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(args[0]);
for(;;)
{
StreamReader sr = ts.Read();
string line;
while((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
sr.Close();
Thread.Sleep(100);
}
}
}
}
 
B

Bert Hyman

In Arne Vajhøj
It was the length not the timestamp I suggested looking at.

Wander around your favorite Web search engine and find the source code
for the tried-and-true *NIX "tail" application.

It does what you want except for the part of monitoring when the writing
application closes the file.

Translating it to C# should be a straightforward process.
 

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