fix a bug in writing output file