Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted
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.

Afraits

"The avalanche has started, it is too late for the pebbles to vote"

Posted

hi,

 

thx for the quick reply.

 

i read the file using a streamreader

 

StreamReader sr = new StreamReader(filePath);

 

and i dont see any method that allows me to get the total of bytes read.

Posted

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.

Afraits

"The avalanche has started, it is too late for the pebbles to vote"

  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

thx for all the replys,

 

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

 

sr.BaseStream.Length

 

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

 

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?

  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...