Page 1 of 2

Needed - file delete and exists commands

Posted: Thu Sep 28, 2017 2:37 am
by rbytes
I modified my FontView program with a Save button so that the user can search for fonts and save their favourite font names and sizes. But once a file is created, the information keeps appending to the file every time the program is run, unless the user deletes it manually.

I would like to be able to create a fresh file each time FontView is run, but I can’t think of a way to do that. Normally I would use code such as if exists(filename) then filename delete. In SPL I imagine it would look more like ? #.exists(filename), #.delete(filename)

Re: Needed - file delete and exists commands

Posted: Thu Sep 28, 2017 3:30 am
by Mr. Kibernetik
Yes, yes. File management functions are next to be done.

Please note that setting file position to the beginning of file makes write functions to write from the beginning (writing over old content):
#.filepos(file,0)

Re: Needed - file delete and exists commands

Posted: Thu Sep 28, 2017 5:13 am
by rbytes
I tried that today. The data wrote into the start of the file, but just appended to the beginning of the old data rather than writing over it.

Re: Needed - file delete and exists commands

Posted: Thu Sep 28, 2017 5:18 am
by rbytes
The other issue is that if I include a filepos command for a file before the user has created the file, the program will stop with an error.

But I will curb my enthusiasm and wait patiently for the new file commands. :)

Re: Needed - file delete and exists commands

Posted: Thu Sep 28, 2017 5:20 am
by Mr. Kibernetik
rbytes wrote:
Thu Sep 28, 2017 5:13 am
I tried that today. The data wrote into the start of the file, but just appended to the beginning of the old data rather than writing over it.
You can give a program sample to show how it goes.

In this code:

Code: Select all

f = "out.txt"
#.writetext(f,"long text")
#.filepos(f,2)
#.writetext(f,"1234")
the contents of "out.txt" file is "1234 text".

Re: Needed - file delete and exists commands

Posted: Thu Sep 28, 2017 5:22 am
by Mr. Kibernetik
rbytes wrote:
Thu Sep 28, 2017 5:18 am
The other issue is that if I include a filepos command for a file before the user has created the file, the program will stop with an error.
Of course it is not possible to set position in non-existing file.

Yes, I just wanted to remind about an existense #.filepos function :D

Re: Needed - file delete and exists commands

Posted: Thu Sep 28, 2017 5:40 am
by rbytes
What if instead of an error, filepos for a non-existent file returned the value -1. That would would eliminate the need for an exists command, but is probably the same amount of work for you. :lol:

Re: Needed - file delete and exists commands

Posted: Thu Sep 28, 2017 5:48 am
by Mr. Kibernetik
Currently supposed file function names are:

#.fdelete
#.frename
#.fmove
#.fcopy
#.fexist

These file functions will be renamed:

#.filepos -> #.fpos
#.filesize -> #.fsize
#.eof -> #.fend

Re: Needed - file delete and exists commands

Posted: Thu Sep 28, 2017 2:15 pm
by rbytes
I ran your test program six times. Here is my out.txt file:

1234 text
long text
long text
long text
long text

It seems that you get overwrites and I get appends.

Re: Needed - file delete and exists commands

Posted: Thu Sep 28, 2017 3:11 pm
by Mr. Kibernetik
rbytes wrote:
Thu Sep 28, 2017 2:15 pm
I ran your test program six times. Here is my out.txt file:

1234 text
long text
long text
long text
long text

It seems that you get overwrites and I get appends.
This is a correct result if to run it many times. Because

Code: Select all

#.writetext(f,"long text")
appends and

Code: Select all

#.fpos(f,2)
#.writetext(f,"1234")
writes from the beginning of file.