Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.
We have no way to know how your code behaves unless you give us examples of the file it reads from.