suve

awful: function: file-force-contents

Available since rev.40 (v.0.5.4).

Declaration

int :file-force-contents ( string $PATH , string $CONTENTS )

Summary

Writes contents of $CONTENTS to the file specified by $PATH. If the file already exists, it is overwritten.
Practically, this is identical to calling :f-rewrite, :f-write and :f-close.

Contrary to :file-put-contents, if any parts of the directory structure specified in $PATH do not exist, this function will perform a :dir-force call in an attempt to create them.

Returns number of bytes written to disk, or -1 on failure.

Example

# :file-force-contents example
!fun :save-config
    :set &json :json-encode $CONFIG
    :set &path :add =CONFIG-PATH =CONFIG-FILE
    :set &fpc :file-force-contents $path $json
    !if :ge $fpc i0
        :writeln s'Configuration saved successfully.'
    !else        
        :writeln s'Unable to save config file!'   
    !fi
!nuf

wikipage modified on 2014/0906/1241