CreateFile()

Syntax

Result = CreateFile(#File, Filename$, Callback [, Flags])
Description
Create an empty file. In SpiderBasic, the file are always created in memory. To be saved, the file needs to be exported using ExportFile() or use the #PB_LocalStorage flag. The file will automatically grows when write commands are used.

Parameters

#File The number to identify the new file. #PB_Any can be used to auto-generate this number.
Filename$ The filename to the new file. It wille be used when using ExportFile()
Callback The callback to be called when data has been saved to the file. It has to use the following syntax:
  Procedure Callback(Status, Filename$, File, SizeRead)
    Select Status
      Case #PB_Status_Saved
        ; File correctly saved
        
      Case #PB_Status_Error
        ; File saving has failed
    EndSelect
  EndProcedure
Flags (optional) It can be a combination (using the '| operand) of the following values (affects the WriteString(), WriteStringN(), ReadString(), ReadCharacter() and WriteCharacter() behaviour):
  #PB_Ascii  : all read/write string operation will use ascii if not specified otherwise.
  #PB_UTF8   : all read/write string operation will use UTF-8 if not specified otherwise (default).
  #PB_Unicode: all read/write string operation will use Unicode if not specified otherwise.
  #PB_LocalStorage: will save the file on client side using its filename when CloseFile() is called. This file
                  could be opened again in anothe session in the same browser later, but it can be wiped if the
                  user clear its local cache. It's also domain related, so if the application domain name change,
                  the files won't be accessible anymore.

Return value

Returns nonzero if the file was created successfully and zero if there was an error. If #PB_Any was used as the #File parameter then the new generated number is returned on success.

Example

  Procedure Callback(Status, Filename$, File, SizeRead)
    Select Status
      Case #PB_Status_Saved
        ; File correctly saved
        
      Case #PB_Status_Error
        ; File saving has failed
    EndSelect
  EndProcedure

  If CreateFile(0, "Text.txt", @Callback())      ; we create a new text file
    For a=1 To 10
      WriteStringN(0, "Line "+Str(a))  ; we write 10 lines (each with 'end of line' character)
    Next
    For a=1 To 10
      WriteString(0, "String"+Str(a))  ; and now we add 10 more strings on the same line (because there is no 'end of line' character)
    Next
    
    ExportFile(0, "text/plain")
    CloseFile(0)                      
  EndIf

See Also

ReadFile(), CloseFile()

Supported OS

All

<- CloseFile() - File Index - Eof() ->