in reply to read and write file in directory

Use strict and warnings will possibly be useful.
Also, a completely self-contained example of the problem would not be remiss here.