Why don't you read the file all at once, then split it to determine the lines?