View Full Version : File Append?
Hi Guys, is there a File Append type command I am not seeing.
That is, I have a text based file, I just want to append content to it at the end WithOUT having to open it, load it up and then add the new content and then save it out again.
Thanks for any info.
Ok, I see I can use File_Open with Append for the mode and then File_LinePrint and then File_Close. Should do the trick for now.
I can't get it to append. I tried seeking to the eof and still no luck.
local s as string
local n as long
s = Str$(planetColorX) + "," + Str$(planetColorY) + "," + Str$(planetColorZ) + "," + Str$(planetX) + "," + Str$(planetY)_
+ "," + BackgroundBMP + $crlf
if (n = FILE_OPEN("LikeSavedSettings.txt", "Append")) then
FILE_Seek(n, FILE_EOF) 'Just added this and still couldn't get it to work, thought it might need it
FILE_LinePrint(n, s)
FILE_CLOSE(n)
end if
Thanks in advance.
ErosOlmi
20-03-2007, 08:39
Ken,
the code
if (n = FILE_OPEN("LikeSavedSettings.txt", "Append")) then
is not returning the file id but comparing the value of n (that is zero at that point) with the fileid returned by file_open.
So you are not storing any file id anywhere.
Also file_open with "APPEND" mode automatically set the file cursor at the end of the file and if the file does not exists, it is created.
So, do it simple, step by step. Open the file and get file id. Test file id and if ok, write what you need and close.
USES "file"
local s as string
local FileID as long
S="123" '--- Write here whatever
FileID = FILE_OPEN(app_soucepath & "LikeSavedSettings.txt", "APPEND")
if FileID then
FILE_LinePrint(FileID, s)
FILE_CLOSE(FileID)
end if
Another info: file_seek will not work for files open in append mode.
Ciao
Eros
Thanks Eros for the very clear explanation and example!!!
ErosOlmi
20-03-2007, 09:11
Ken,
in thinBasic an assignement directive starts with a variable.
So,
n = FILE_OPEN("LikeSavedSettings.txt", "Append")
is an assignment directive, while
if (n = FILE_OPEN("LikeSavedSettings.txt", "Append")) then
is a comparison directive. N will not given a value but the value of N is compared with the value returned by File_Open but never assigned to N.
Other languages have = or := to assign and == to compare. Or even other mix.
In thinBasic we want it simple and easy to understand so most of the time it is better to just write different sentence: one for assignement and one for comparing.
Ciao
Eros
I agree, it does make it so much easier to follow what is happening. Look at the mistake I made otherwise, trying to combine it as I did. Thanks again Eros.
ErosOlmi
21-03-2007, 00:57
Ken,
I've added FILE_Append (http://www.thinbasic.com/public/products/thinBasic/help/html/file_append.htm) function.
Should be more easy to use.
Ciao
Eros
Thanks Eros, I love being spoiled with such goodies, couldn't be any easier!!