suve

awful: function: file-put-contents

Available since rev.40 (v.0.5.4).

Declaration

int :file-put-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.

Returns number of bytes written to disk, or -1 on failure. Note that the directory structure must exist for this function to succeed.

Example

# :file-put-contents example
!fun :save-config
    !if :not :dir-exists =CONFIG-PATH
        !if :dir-force =CONFIG-PATH
            :writeln s'Config directory created successfully.'
        !else
            :writeln s'Unable to create config directory!'
            :return
        !fi
    !fi          
 
    :set &json :json-encode $CONFIG
    :set &path :add =CONFIG-PATH =CONFIG-FILE
 
    :set &fpc :file-put-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/1244