fix length calculation in file write