What if you don't close the file before writing to it?

PS. You don't have to allocate everything on the heap..