use strlen rather than sizeof when writing
close files after operation