Apologies for not scrutinizing your program better earlier. Still, after looking at it, I am a bit puzzled as I thought your code "reads" a file, yet I don't see the filehandle for the input.
For reading a file line by line, the usual prescription is: