Write not appending with READ.WRITE

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By cgeadas

With the following code I’m having the file truncated, not appended.

	if ficheiro.open(nome_ficheiro, File.READ_WRITE) == OK:

Is this supposed to erase the contents of the file?
I had the idea that, for truncating, there is WRITE_READ. Is this a bug?

Also, is there a way to create a file if it does not exist and ONLY append if it exists?
(I mean another way besides reading the file and writing it back all together)

I think the string has to be in quotations

Merlin1846 | 2020-03-20 03:29

But this code writes the string successfully.
The point is that it does not append a new line to the file as it should but instead deletes the string it was saved there before and writes the new one. So, instead of getting a file with a content like this:

I get:

cgeadas | 2020-03-20 04:32

:bust_in_silhouette: Reply From: wombatstampede

You can use the method .seek_end() to move to the end of file. If you don’t position inside the file it will (probably) overwrite the file from the beginning.

You’re right.
I’ve ended up with:

	if ficheiro.file_exists(nome_ficheiro):
		stat = ficheiro.open(nome_ficheiro, File.READ_WRITE)
		stat = ficheiro.open(nome_ficheiro, File.WRITE)
	if stat == OK:

This does it.

cgeadas | 2020-03-20 18:53