Runtime Error 5 means Access denied.
The file maybe readonly and you use the wrong (default) filemode, or you try to re-open the file with a new filehandle without having closed it before (somewhere in the while and repeat loops possibly you assignfile more then once, then the reset fails?).
Step through the code to see where exactly the runtime error occurs.
You might also consider tesing if file IO succeded before trying to do something with the data.
Either use exceptions, or use the {$I-} and {$I+} compiler directives and check IOResult after each File IO.
Hope this helps a little?
Bart