equivalent to SetEndOfFile_()?

Just starting out? Need help? Post your questions and find answers here.
morosh
Posts: 27
Joined: Mon Feb 02, 2015 7:48 pm

equivalent to SetEndOfFile_()?

Post by morosh »

Hello:

is there any equivalent to the win api SetEndOfFile_()?

thanks
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: equivalent to SetEndOfFile_()?

Post by Peter »

SetEndOfFile_() of which file?

You surely know that for security reasons you cannot access local files from your browser?

Greetings ... Peter
User avatar
Danilo
Posts: 51
Joined: Wed Feb 26, 2014 7:11 am

Re: equivalent to SetEndOfFile_()?

Post by Danilo »

morosh wrote:is there any equivalent to the win api SetEndOfFile_()?
The following function truncates the file at the current file pointer location (FileSeek()).
You can truncate a file (make it smaller), but you can't extend the file size (make it larger).

Code: Select all

Procedure TruncateFile(file)
    !var f;
    !if (f = spider.file.objects.Get(v_file)) f.size = spider_Loc(v_file);
EndProcedure

file = CreateFile(#PB_Any,"",0)
If file
    WriteString(file,"0123456789",#PB_Unicode)
    
    Debug "Length of file:       " + Lof(file)
    Debug "Current file pointer: " + Loc(file)
    
    FileSeek(file,8)
    TruncateFile(file)
    
    ;WriteString(file,"Hello!",#PB_Unicode)
    ;WriteWord(file,0)
    
    Debug "Length of file:       " + Lof(file)
    Debug "Current file pointer: " + Loc(file)
    
    export = ExportFileMemory(file)
    If export
        Debug "-----"
        Debug "Length of memory: " + MemorySize(export)
        Debug PeekS(export,0,MemorySize(export),#PB_Unicode)
    EndIf
    CloseFile(file)
EndIf
cya,
...Danilo
User avatar
Danilo
Posts: 51
Joined: Wed Feb 26, 2014 7:11 am

Re: equivalent to SetEndOfFile_()?

Post by Danilo »

Renamed SetEndOfFile() to TruncateFile(), so it is compatible with PureBasic. ;)
cya,
...Danilo
morosh
Posts: 27
Joined: Mon Feb 02, 2015 7:48 pm

Re: equivalent to SetEndOfFile_()?

Post by morosh »

Thank you Danilo
Perfect, exactly what I'm searching for.
By the way, I need also to CopyFile which is simple to implement (copying line by line), but is there something shorter with JS.
Also later I need to delete temporary files, no delete actually available

Regards
User avatar
Danilo
Posts: 51
Joined: Wed Feb 26, 2014 7:11 am

Re: equivalent to SetEndOfFile_()?

Post by Danilo »

What type of files do you work with, morosh? #PB_LocalStorage files?

Looks like DeleteFile() is missing from SB. For now you could overwrite it using CreateFile():CloseFile() to create a new file with size 0.

EDIT:

Code: Select all

;
; SpiderBasic 2.30 - Delete a file that was created using '#PB_LocalStorage' flag.
;
; ref: https://localforage.github.io/localForage/
;
Procedure DeleteFile(filename.s, flag.i = #PB_LocalStorage)
    If flag = #PB_LocalStorage
        !LocalForage.removeItem("sbfs_" + v_filename);
    EndIf
EndProcedure
cya,
...Danilo
morosh
Posts: 27
Joined: Mon Feb 02, 2015 7:48 pm

Re: equivalent to SetEndOfFile_()?

Post by morosh »

thank you Danilo
my files are on the server side, sometimes I need to do some processing, so I need to create a temporary file, fill it correctly, then copy it to the main file, then delete it (always server side).

Regards
Post Reply