We showed a similar example here, where you basically are reading and writing to the same file to increment a counter:
That example overwrites the file each time, so that the counter increments. But you could take the same pattern to append to a file, where you write each additional record to the end of the file.