Click to See Complete Forum and Search --> : Huge files problem


clatten
September 3rd, 2004, 05:58 AM
I am working on an app that should open a logfile and then it should be possible to:

1. output the whole file as it is
2. reverse the output
3. filter the output

I am outputting to a richtextbox since I also want to highlight specific lines.

I have tried both using ArrayList and StringBuilder. The latter seems better but then it is not so easy to reverse. To add all text to the RichTextBox I use RTB.Text. I have also thought of outputting all to a temp file and use RTB.LoadFile instead.

The problem is that the logfiles can be quite huge, >5MB in extreme cases. This causes it to be very slow and I also encounter out-of-index problems in certain situations that I have not been able to pin down in the debugger.

Could anyone advice me on the best way to get a stable and not to slow solution (since I have to work through the whole file line by line to filter it will never be quick)?

mehdi62b
September 3rd, 2004, 03:46 PM
I cant get where you have this problem exactly,although I dont know how you load this huge file,

I think if you use stream and streamreader it would be better ...

if I'm wrong please notice me,
any better opinions for such situations??

---------------
Mehdi.:)

clatten
September 3rd, 2004, 04:55 PM
Sorry about that, should have been more informative I guess. The code looks like this (You can see both approaches, the arraylist and stringbuilder):


try
{
StreamReader sr = File.OpenText(fileName);
string input = null;
//ArrayList myList = new ArrayList();
StringBuilder sb = new StringBuilder();

while ((input = sr.ReadLine()) != null)
{
if ( Filter or not )
{
Filtering of each line. If condition met then
sb.Append(input);
//myList.Add(input);
}
else {
sb.Append(input);
//myList.Add(input);
//myList.TrimToSize();
}

}
sr.Close();
//myList.TrimToSize();
if ( checkBox4.Checked ) { //myList.Reverse(); }
richTextBox1.Clear();

/*
for (int i=0;i<myList.Count;i++)
{
sb.Append(myList[i].ToString() + "\n");
}
*/
richTextBox1.Text = sb.ToString();

}
catch (Exception e)
{ MessageBox.Show(e.ToString()); }
}

It seems as the StringBuilder solution is better but I don't know how to reverse in a nice way, since the java stringbuilder.reverse does not seem to exist in C#