Click to See Complete Forum and Search --> : Infinite Loop text file read


yraen
April 14th, 2008, 07:12 PM
What I'm trying to do is create an app that will open a text file (that is currently being appended to by another program), read the newest lines, pull information from those lines into various variables, and then wait until more lines are added.

I've got the opening of the file and the variable comparisons working fine... what I can't get is how to make it read only the lines from the time my app is started until I close it and pull the information from those lines.

I'm using a StreamReader... and my code (plus attempted codes, yes it's ugly at the moment) looks like:

//while ((strLine = sr.ReadLine()) != null)
//buffersize = 4096;
//char[] buffer = new char[buffersize];

//while (1 != 2)
//{
fs = new FileStream(strPath2, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
sr = new StreamReader(fs);
//sr.BaseStream.Seek(intLength1, SeekOrigin.Current);
intLength2 = Convert.ToInt32(sr.BaseStream.Length);
while (intLength2 > intLength1)
{
long intStore = Convert.ToInt64(intLength2 - intLength1);
buffersize = intLength2;
char[] buffer = new char[buffersize];
//sr.BaseStream.Seek(intStore, SeekOrigin.Begin);

//sr.BaseStream.Position = intLength1;
intLength1 = Convert.ToInt32(sr.BaseStream.Length);
sr.BaseStream.Seek(intStore, SeekOrigin.End);
strLine = Convert.ToString(sr.Read(buffer, intLength1, Convert.ToInt32(intStore)));
//sr.BaseStream.SetLength(Convert.ToInt64(intStore));
while ((strLine = sr.ReadLine()) != null)
//the rest of my code

Anyone have any tips on how to make this work?

ALdeC
April 14th, 2008, 10:10 PM
add indicator in your text file (for example line number), then that will be your reference in getting only the lines from the time your application started until you closed it.

yraen
April 15th, 2008, 01:27 PM
add indicator in your text file (for example line number), then that will be your reference in getting only the lines from the time your application started until you closed it.
If you're saying to add a line number to the actual text file, that won't work as it is constantly being modified by another application.

I was given the idea of using the file size to get my starting point and telling my app when to start the read, however I can't seem to get that to work correctly. I can get it to kind of work by reading the lines to an array and comparing each element... but it take 30-50% of the CPU and seems to miss quite a few lines.

Here's what I have that "kind of" works:

fs = new FileStream(strPath2, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
sr = new StreamReader(fs);
strLength1 = Convert.ToString(sr.BaseStream.Length);
fs.Close();
while (1 != 2)
{
fs = new FileStream(strPath2, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
sr = new StreamReader(fs);
strLength2 = Convert.ToString(sr.BaseStream.Length);
while (Convert.ToInt32(strLength2) > Convert.ToInt32(strLength1))
{
long lngStore = Convert.ToInt64(strLength2) - Convert.ToInt64(strLength1);
sr.BaseStream.Seek(-lngStore, SeekOrigin.End);
sr.BaseStream.Position = Convert.ToInt64(strLength1);
strLine = sr.ReadToEnd();
Array aLines = strLine.Split('\r');
strLength1 = Convert.ToString(sr.BaseStream.Length);
//The rest of my code

Any better ideas? :o

darwen
April 15th, 2008, 03:14 PM
Copy the file to another file, and then read that.

For instance, if the copied file is called 'copy.txt'.

(1) Read the size of 'copy.txt'.
(2) Copy the constantly-being-written-to file to 'copy.txt', overwriting it.
(3) Start reading 'copy.txt' after the end of the original 'copy.txt'.

Get the idea ?

This'll also stop the file from changing beneath your feet when you're reading it.

Darwen.

yraen
April 15th, 2008, 03:22 PM
Copy the file to another file, and then read that.

For instance, if the copied file is called 'copy.txt'.

(1) Read the size of 'copy.txt'.
(2) Copy the constantly-being-written-to file to 'copy.txt', overwriting it.
(3) Start reading 'copy.txt' after the end of the original 'copy.txt'.

Get the idea ?

This'll also stop the file from changing beneath your feet when you're reading it.

Darwen.
Sounds plausible, just wondering how efficient it will be as some of the text files can be 100's of mb in size...

I'll give it a try and see how it goes though, thanks :)