adds basic error checking
opens file only if there's something to write