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

equivalent to SetEndOfFile_()?

by morosh Tue Nov 26, 2019 5:49 pm

Hello:

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

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

Re: equivalent to SetEndOfFile_()?

by Peter Tue Nov 26, 2019 6:11 pm

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: 49
Joined: Wed Feb 26, 2014 7:11 am

Re: equivalent to SetEndOfFile_()?

by Danilo Wed Nov 27, 2019 6:09 am

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: 49
Joined: Wed Feb 26, 2014 7:11 am

Re: equivalent to SetEndOfFile_()?

by Danilo Wed Nov 27, 2019 8:10 pm

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

Re: equivalent to SetEndOfFile_()?

by morosh Thu Nov 28, 2019 5:33 pm

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: 49
Joined: Wed Feb 26, 2014 7:11 am

Re: equivalent to SetEndOfFile_()?

by Danilo Fri Nov 29, 2019 5:36 am

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: 23
Joined: Mon Feb 02, 2015 7:48 pm

Re: equivalent to SetEndOfFile_()?

by morosh Fri Nov 29, 2019 12:04 pm

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
Return to Coding Questions

Who is online

Users browsing this forum: No registered users and 4 guests