writeResults function

Write query results to a file.