Just sharing some of my inconsequential lunch conversations with you... RSS  

Friday, October 12, 2007

LINQ-compatible streaming I/O helper

When reading some of the Wide Finder implementations, I come across with Don Box's approach. Besides the Wide Finder problem (be sure to look for this and other languages implementations), he gave us a nice clean generator based ReadLinesFromFile implementation. Here it is:




// LINQ-compatible streaming I/O helper

public static IEnumerable<string> ReadLinesFromFile(string filename)

{

using (StreamReader reader = new StreamReader(filename))

{

while (true)

{

string s = reader.ReadLine();

if (s == null)

break;


yield return s;

}

}

}


3 comments:

Paddy3118 said...

(Your long lines have been chopped by this rubbish blogger software)!

Mário Romano said...

Your right, my template choice was not the best to present code. I'll try to tweak this one, and if it doesn't work, change the template.

Thanks for your comment.

Mário Romano said...

Here's the tweaking: http://devcatharsis.blogspot.com/2007/10/format-code-snippets-in-blogs.html

Development Catharsis :: Copyright 2006 Mário Romano