المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Progress while reading a file



C# Programming
04-16-2009, 01:01 AM
Hi! I have to read a file and do some operations to it. The contents of the file are words - it is like a dictionary (over 10 000 words, later over 80 000). Each word must be read and compared to others. Which is the best way of accessing the data (and the fastest):
1) Read the whole file and add the lines to an ArrayList, after that the stream is disposed. Then do whatever I want to the lines.
OR
2) Open the stream and read each line and do whatever I want to it. During all the time the stream is opened!

Also I want to report progress.
1) With the the ArrayList:

int progress = 0;
for (int i = 0; i < rawLines.Count; i++)
{
line = rawLines[i] as string;
if ((i / rawLines.Count) * 100 > progress)
{
progress = (i / rawLines.Count) * 100; //the new progress value
Console.Title = progress.ToString() + "%";
}
//
//some stuff to the line
//
}

2) With the stream:

int progress = 0;
int rawProgress;
while ((line = reader.ReadLine()) != null)
{
rawProgress = (int)((reader.BaseStream.Position / reader.BaseStream.Length) * 100);
if (rawProgress > progress)
{
progress = rawProgress; //the new progress value
Console.Title = progress.ToString() + "%";
}
//
//some stuff to the line
//
}

But! Both ways don't seem to work. With the stream, the value of reader.BaseStream.Position is always 1024. In the other case the value of i increases but the percentage in the console's title does not change?!

Still learning...