fix error in if else, begin write data to external file