Append data to a file

Discuss any general programming issues here
Post Reply
User avatar
JurassicC
Posts: 36
Joined: Fri May 25, 2012 9:48 pm

Append data to a file

Post by JurassicC »

Is there any easy way to open a file and append data to the end of it instead of overwriting it ?
Essentially I would like to open an already existing text file, move to the end position and start writing data on a new line at the end of the file using WriteLine() ?

Edit forgot so say I'm doing this to try to get to the end of the file

OpenFile (1,"DB.CSV",#MODE_WRITE)
Seek(1,#EOF)
DebugPrint(FilePos(1))

Always Returns 0 as the filepos

Edit never mind I just discovered i am using the wrong #mode, should be #MODE_READWRITE :oops:
Last edited by JurassicC on Thu May 15, 2014 10:59 pm, edited 1 time in total.
AmigaOne X1000 - RadeonHD 7870, 4GB RAM - AmigaOS 4.1ß
AmigaOne X5000 - Radeon R7 250, 2GB RAM - AmigaOS 4.1ß
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Append data to a file

Post by airsoftsoftwair »

You have to use #MODE_READWRITE and there's also a bug in the way you call Seek(). The constant #EOF is not recognized by Seek(). You have to pass an absolute seeking position.
User avatar
JurassicC
Posts: 36
Joined: Fri May 25, 2012 9:48 pm

Re: Append data to a file

Post by JurassicC »

So do I get that from reading the file length first or do a readchr until I get to the EOF and use the amount of characters for Seek()

I'm doing to do this now is there a better way to get the EOF position.


OpenFile (2,"DB.CSV",#MODE_READWRITE)
MyFilePos = 0
While Not EOF(2)
Seek(2,MyFilePos)
MyFilePos = MyFilePos +1
Wend
DebugPrint(MyFilePos)
AmigaOne X1000 - RadeonHD 7870, 4GB RAM - AmigaOS 4.1ß
AmigaOne X5000 - Radeon R7 250, 2GB RAM - AmigaOS 4.1ß
User avatar
JurassicC
Posts: 36
Joined: Fri May 25, 2012 9:48 pm

Re: Append data to a file

Post by JurassicC »

Got it working if I close the file and reopen and seek to MyFilePos-1 it all good and I'm appending data.

Cheers for the Tips
AmigaOne X1000 - RadeonHD 7870, 4GB RAM - AmigaOS 4.1ß
AmigaOne X5000 - Radeon R7 250, 2GB RAM - AmigaOS 4.1ß
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Append data to a file

Post by airsoftsoftwair »

Code: Select all

MyFilePos = 0
While Not EOF(2)
Seek(2,MyFilePos)
MyFilePos = MyFilePos +1
Wend
Phew, this code is really ugly! :) Just get the file size using FileSize() before opening the file and you're done...
Post Reply