Iterating through file

  • Thread starter Thread starter Jerry S
  • Start date Start date
J

Jerry S

Hi

I've got a text file which is a list of names (one name per line). I
don't know in advance how many names (lines) there are in the file.

What's the easiest way to iterate through the file, doing something
with each name?

Is there a "while file not ended" type command? (I understand about
stream readers, etc., I'm just not sure about what kind of loop to
use!).

Thanks!

Jerry
 
Jerry S said:
Hi

I've got a text file which is a list of names (one name per line). I
don't know in advance how many names (lines) there are in the file.

What's the easiest way to iterate through the file, doing something
with each name?

Is there a "while file not ended" type command? (I understand about
stream readers, etc., I'm just not sure about what kind of loop to
use!).

string strLine = reader.ReadLine();
while (strLine != null) {
if (strLine.Length > 0) {
// Do your task for each file name
}
strLine = reader.ReadLine();
}
 
Wouldn't you make things easier by using a binary file?
That way you won't have problems updating your data.

If your data file is very big, you could then create a second index
file with the names and the record numbers of the data file.
You read through the index file sequentially and then go directly
(randomly) to the data file.
 
Wouldn't you make things easier by using a binary file?
That way you won't have problems updating your data.

If your data file is very big, you could then create a second index
file with the names and the record numbers of the data file.
You read through the index file sequentially and then go directly
(randomly) to the data file.
 
Back
Top