Still looking at this...
However let me pick the first nit.
Why are you reading the file? If the program on the target system is
truly a 'trigger' program it will contain the entire record buffer as one
of the parameters to the trigger program. That has all the data you need.
If you do not have to read the file you'll never have I/O to the file.
Thus the file will not be left open.