Progress Bar And Tex files

rfazendeiro

Centurion
Joined
Mar 8, 2004
Messages
110
Hi to all,

Currently in my application i have to read from a huge file and make some operations with the file. Because the file takes some time to read, i would like to show a progress bar to indicate that the file is being read.

My problem is that i dont know how many lines the file has so i cannot indicate the maximun value to increment it.

any body has an ideia?
 
You should be able to get the total file length and the number of bytes read in each line - hence display the bar as the proportion of bytes read rather proportion of lines read.
 
hi,

thx for the quick reply.

i read the file using a streamreader

Code:
StreamReader sr = new StreamReader(filePath);

and i dont see any method that allows me to get the total of bytes read.
 
Use the Fileinfo class to get information about the file before opening it, or use the basestream property of the streamreader which should then provide access to the base Filestream property of length, the filestream class also exposes a property position which should give the byte position within the file.

Alternatively you can count the number of bytes in each line of data that you read and track the position that way.
 
If you can't determine the exact count easily, you can use the "web progress bar" trick. That is, have the progress change on a timer or some interval - it goes back and forth, instead of just filling to the right. It's basically just some movement on the screen to let people know that things are working.

How big is the file, would you guess? If it's less than a hundred meg you may consider reading the whole file into a string. Then you could count the number of EOL characters (char 10 or 13 or a combination, depending on the file). Kinda cheesy, but throwing out suggestions.

If this is a fixed width file, I would definitely use the FileInfo object to get the FileSize - the OS can provide that without having to read in the whole file.

-ner
 
thx for all the replys,

i can get the size of the hole file. The StreamReader provides me thar with

Code:
sr.BaseStream.Length

the problem is getting the number of bytes of a line. I have sothing like this

Code:
private string ProcessApolice()
{
             //Object tha will hold the information from the file
	Apolices novaApolice = new Apolices();

             //Open the file. CINICIAL_FICH_APOLICES is a constante wih the path of the file
	StreamReader srApolices = new StreamReader(CINICIAL_FICH_APOLICES);


	string sLineApolices = null;

	while((sLineApolices = srApolices.ReadLine()) != null)
	{
		//Put the information in the object
		novaApolice.companhia  = StringHandler.sGetfield(sLineApolices, 0, 10);
		novaApolice.apolice = StringHandler.sGetfield(sLineApolices, 10, 20);
		novaApolice.ramo = StringHandler.sGetfield(sLineApolices, 30, 10);
		novaApolice.produto = StringHandler.sGetfield(sLineApolices, 40, 10);
		novaApolice.codCliente = StringHandler.sGetfield(sLineApolices, 50, 10);
		
		srApolices.Close();
	}
}


//Gets the substring i want
public static string sGetfield(string sline, int since, int until)
{
	if ((sline != string.Empty) && (sline != null))
		return sline.Substring(since, until);
	else
		return null;
}


now what i needed is to know the lenght in bytes of sLine. Does anyone have a ideia?
 
You can use .Length on any string to get the length. But this technique will only work if this is a fixed-width file, meaning each line is the exact same length. If it is then you don't want to use sLine.Length; use a constant that has the length of each line.

To get the total number of lines: FileSize / LineWidth (the constant).
Don't forget to add 1 or 2 to LineWidth to account for the EOL character(s) (sometimes 1, sometimes 2).

-ner
 
Hummm....let me see now....

Yes...of course....you're right....how stupid...the solution was right in my face!! each line have always the same length

thx for the help!
 
Back
Top